概要:
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 'discount price' from products where id <=15;
5、WHERE可以在SELECT/UPDATE/DELETE中
a)可使用的邏輯運算符號(將多個條件組合)
&&/AND ||/OR !/NOT
b)可使用的比較運算符號
=#判斷是否相等,與程序中的==作用相同
<=>#判斷是否相等,與=一致,但可以用於與NULL比較
!=/ <> #不等號
<
<=
>
>=
c)程序中沒有的運算符
ISNULL #與'<=>NULL' 相等
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 '______'; #查找任意名字為6個字符的數據
select* from products where name like '%java%'; #查詢名字中包含有java的數據
NOTLIKE
e.g. select* from products where name not like '%java%'; #查詢名字中不包含java字樣的數據。
REGEXP/RLIKE【正則表達式】 #RegExp 正則表達式
e.g. select* from products where name regexp '^java'; #查找所有以java開頭的數據
select* from products where name regexp 's$'; #查找所有以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'^j') ;
select* from products where cid in(select id from carts where name like'j%'); #作用相同
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結合才能使用