標籤:mysql sql 設計 select 查詢
SQL語言的設計與編寫(下)--SELECT查詢精講
概要:
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【Regex】 #RegExpRegex
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結合才能使用
MySQL學習筆記_6_SQL語言的設計與編寫(下)