概要:
SELECT[ALL | DISTINCT] #distinct 明显的,清楚的,有区别的
{*|table.*|[table.]field1[asalias1][,[table.]field2[as alias2]][.....]} #alias 别名,化名
FROM 表名
[WHERE...]
[GROUPBY...]
[HAVING...]
[ORDERBY ...]
[LIMITcount]
使用SELECT查询语言,目的是可以按用户的想法将数据查出来,将结果返回!
1、字段要列出要查询的字段
e.g. selectname,price from products;
selectprice,name from products;
select* from products;
selectproducts.* from products; #单表其实不需要使用表名
2、可以为每个字段起个别名【后面会用到(关键字,多表查询)】【表也可起别名(多表查询)】
e.g. selectname as bookname,price as bookprice from products;#使用别名;也可不加as;注意别名中有空格时,需要加单引号;
3、使用distinct作用与整个记录,取消重复的数据,只返回一个,而不是单独的一列
e.g. selectdistinct price 'book price' from products;
4、在SQL语句中使用表达式的列(可以使用算术运算符,条件运算符,逻辑运算符...)
e.g. select1+2*3;
select8%5
updateproducts set num = num + 1 where id = 22;
selectname,price,price*0.7 as &#39;discount price&#39; from products where id <=15;
5、WHERE可以在SELECT/UPDATE/DELETE中
a)可使用的逻辑运算符号(将多个条件组合)
&&/AND ||/OR !/NOT
b)可使用的比较运算符号
=#判断是否相等,与程序中的==作用相同
<=>#判断是否相等,与=一致,但可以用于与NULL比较
!=/ <> #不等号
<
<=
>
>=
c)程序中没有的运算符
ISNULL #与&#39;<=>NULL&#39; 相等
ISNOT NULL
BETWEENAND
e.g. select* from products where id between 10 and 20;
与 “select* from products where id >= 10 && id <= 20;”作用相同
NOTBETWEEN AND
IN
e.g. select* from products where id in(5,10,15,20);
updateproducts set num = 77 where id in(5,10,15,20);
deletefrom products where id in(5,10);
d)模糊查询
LIKE _(任意一个字符)和%(0个或多个任意字符)两个通配符号
e.g. select* from products where name like &#39;______&#39;; #查找任意名字为6个字符的数据
select* from products where name like &#39;%java%&#39;; #查询名字中包含有java的数据
NOTLIKE
e.g. select* from products where name not like &#39;%java%&#39;; #查询名字中不包含java字样的数据。
REGEXP/RLIKE【正则表达式】 #RegExp 正则表达式
e.g. select* from products where name regexp &#39;^java&#39;; #查找所有以java开头的数据
select* from products where name regexp &#39;s$&#39;; #查找所有以s结尾的数据
6、多表查询(连接查询),比较常用 #ambiguous
e.g. selectcats.name,products.name from cats,products;
selectc.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p;#将A表中的记录与B表中的记录依次匹配,得到A*B种结果【笛卡尔乘积】,该结果是没有意义的。
selectc.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p where c.id=p.cid;
selectc.name cname,c.desn cdesn,p.name pname,p.price,p.desn pdesn,p.numfrom carts c,products as p where c.id=p.cid and c.id=3;
selecta.id aid,a.name aname,b.id bid,b.name bname from cats a,catsb; #将单表分为多表,进行查询
selecta.id aid,a.name aname,b.id bid,b.name bname from cats a,cats b wherea.pid = b.id;
7、嵌套查询子查询
e.g. select* from products where cid in(select id from carts where name regexp&#39;^j&#39;) ;
select* from products where cid in(select id from carts where name like&#39;j%&#39;); #作用相同
8、orderby 字段 [asc正序]desc倒序
e.g. select* from order by name;
select* from order by price; #按价格非递减排序
select* from order by price desc; #非递增排序
select* from where cid > 5 order by price desc; #与where结合使用
9、limitcount【限制显示个数】
e.g. select* from limit 7;
select* from order by id desc limit 7;
select* from where id <10 order by id desc limit 7;
select* from where id > 14 order by id asc limit 0,1; #limit0,1表示从第0个开始取,取1个
10、groupby 字段【分组】
常用函数:
count() #一个字段的总数
sum()
avg()#平均值
max()
min()
e.g. selectcount(*),sum(price),avg(price),max(price),min(price) from products;
selectcid,count(price),sum(price),avg(price),max(price),min (price) fromproducts group by cid;
selectcid,count(price),sum(price),avg(price),max(price),min (price) fromproducts group by cid having avg(price) > 50; #加having条件,与where类似
#having必须与gropby结合才能使用
bitsCN.com