|
一、mysql查詢的五種子句 where(條件查詢)、having(篩選)、group by(分組)、order by(排序)、limit(限制結(jié)果數(shù)) 1、where常用運(yùn)算符: ※比較運(yùn)算符
> , < ,= , != (< >),>=
, <= in(v1,v2..vn)
between
v1 and v2 在v1至v2之間(包含v1,v2) ※邏輯運(yùn)算符 not ( ! ) 邏輯非 or ( || )
邏輯或 and (
&& ) 邏輯與 where price>=3000 and price <=
5000 or price >=500 and price <=1000 ,取500-1000或者3000-5000的值 where price not
between 3000 and 5000不在3000與5000之間的值 ※模糊查詢 like 像 通配符: % 任意字符 _單個(gè)字符 where
goods_name like '諾基亞%' where
goods_name like '諾基亞N__' 2、group
by 分組 一般情況下group需與統(tǒng)計(jì)函數(shù)(聚合函數(shù))一起使用才有意義 如:select goods_id, goods_name, cat_id, max(shop_price) from goods group by cat_id; 這里取出來(lái)的結(jié)果中的good_name是錯(cuò)誤的!因?yàn)?/span>shop_price使用了max函數(shù),那么它是取最大的,而語(yǔ)句中使用了group by 分組,那么goods_name并沒(méi)有使用聚合函數(shù),它只是cat_id下的第一個(gè)商品,并不會(huì)因?yàn)?/span>shop_price改變而改變。 mysql中的五種統(tǒng)計(jì)函數(shù): (1)max:求最大值 select max(goods_price) from goods 這里會(huì)取出最大的價(jià)格的值,只有值 #查詢每個(gè)欄目下價(jià)格最高的 select
cat_id,max(goods_price) from goos group by cat_id; #查出價(jià)格最高的商品編號(hào) select goods_id,max(goods_price) from goods
group by goods_id; (2)min:求最小值 (3)sum:求總數(shù)和 #求商品庫(kù)存總和 select sum(goods_number) from goods; 4)avg:求平均值 #求每個(gè)欄目的商品平均價(jià)格 select
cat_id,avg(goods_price) from goods group by cat_id; (5)count:求總行數(shù) #求每個(gè)欄目下商品種類 select cat_id,count(*)
from goods group by cat_id; ※要把每個(gè)字段名當(dāng)成變量來(lái)理解,它可以進(jìn)行運(yùn)算※ 例:查詢本店每個(gè)商品價(jià)格比市場(chǎng)價(jià)低多少; select
goods_id,goods_name,goods_price-market_price from goods; 查詢每個(gè)欄目下面積壓的貨款 select
cat_id,sum(goods_price*goods_number) from goods group by cat_id; ※可以用as來(lái)給計(jì)算結(jié)果取個(gè)別名※ select cat_id,sum(goods_price *
goods_number) as hk from goods group by cat_id 不僅列名可以取別名,表單也可以取別名 3、having 與where 的異同點(diǎn) having與where類似,可以篩選數(shù)據(jù),where后的表達(dá)式怎么寫,having后就怎么寫
where針對(duì)表中的列發(fā)揮作用,查詢數(shù)據(jù)
having對(duì)查詢結(jié)果中的列發(fā)揮作用,篩選數(shù)據(jù) ※查詢本店商品價(jià)格比市場(chǎng)價(jià)低多少錢,輸出低200元以上的商品
select goods_id,good_name,market_price - shop_price as s from goods
having s>200 ;
//這里不能用where因?yàn)?/span>s是查詢結(jié)果,而where只能對(duì)表中的字段名篩選 如果用where的話則是:
select goods_id,goods_name from goods where market_price - shop_price
> 200; ※同時(shí)使用where與having
select cat_id,goods_name,market_price - shop_price as s from goods where
cat_id = 3 having s > 200; ※查詢積壓貨款超過(guò)2萬(wàn)元的欄目,以及該欄目積壓的貨款
select cat_id,sum(shop_price * goods_number) as t from goods group by
cat_id having s > 20000 ※查詢兩門及兩門以上科目不及格的學(xué)生的平均分 思路: ※先計(jì)算所有學(xué)生的平均分
select name,avg(score) as pj from stu group by name; ※查出所有學(xué)生的掛科情況 select
name,score<60 from stu; //這里score<60是判斷語(yǔ)句,所以結(jié)果為真或假,mysql中真為1假為0 ※查出兩門及兩門以上不及格的學(xué)生
select name,sum(score<60) as gk from stu group by name having gk >
1; ※綜合結(jié)果
select name,sum(score<60) as gk,avg(score) as pj from stu group by
name having gk >1; 4、order by (1) order by price //默認(rèn)升序排列 (2)order by price desc //降序排列
(3)order by price asc //升序排列,與默認(rèn)一樣 (4)order by rand() //隨機(jī)排列,效率不高 #按欄目號(hào)升序排列,每個(gè)欄目下的商品價(jià)格降序排列 select * from goods where
cat_id !=2 order by cat_id,price desc; 5、limit limit [offset,] N offset 偏移量,可選,不寫則相當(dāng)于limit 0,N N 取出條目 ※取價(jià)格第4-6高的商品
select good_id,goods_name,goods_price from goods order by good_price desc
limit 3,3; ###查詢每個(gè)欄目下最貴的商品 思路: #先對(duì)每個(gè)欄目下的商品價(jià)格排序 select
cat_id,goods_id,goods_name,shop_price from goods order by cat_id,shop_price
desc; #上面的查詢結(jié)果中每個(gè)欄目的第一行的商品就是最貴的商品 #把上面的查詢結(jié)果理解為一個(gè)臨時(shí)表[存在于內(nèi)存中]【子查詢】 #再?gòu)呐R時(shí)表中選出每個(gè)欄目最貴的商品 select * from
(select goods_id,goods_name,cat_id,shop_price from goods order by cat_id,shop_price
desc) as t group by cat_id; #這里使用group by
cat_id是因?yàn)榕R時(shí)表中每個(gè)欄目的第一個(gè)商品就是最貴的商品,而group by前面沒(méi)有使用聚合函數(shù),所以默認(rèn)就取每個(gè)分組的第一行數(shù)據(jù),這里以cat_id分組
良好的理解模型: 1、where后面的表達(dá)式,把表達(dá)式放在每一行中,看是否成立 2、字段(列),理解為變量,可以進(jìn)行運(yùn)算(算術(shù)運(yùn)算和邏輯運(yùn)算) 3、 取出結(jié)果可以理解成一張臨時(shí)表 二、mysql子查詢 1、where型子查詢 (把內(nèi)層查詢結(jié)果當(dāng)作外層查詢的比較條件)
#不用order by 來(lái)查詢最新的商品
select goods_id,goods_name from goods where goods_id = (select
max(goods_id) from goods); #取出每個(gè)欄目下最新的產(chǎn)品(goods_id唯一)
select cat_id,goods_id,goods_name from goods where goods_id in(select
max(goods_id) from goods group by cat_id);
2、from型子查詢 (把內(nèi)層的查詢結(jié)果供外層再次查詢) #用子查詢查出掛科兩門及以上的同學(xué)的平均成績(jī) 思路: #先查出哪些同學(xué)掛科兩門以上 select
name,count(*) as gk from stu where score < 60 having gk >=2; #以上查詢結(jié)果,我們只要名字就可以了,所以再取一次名字 select
name from (select name,count(*) as gk from stu having gk >=2) as t; #找出這些同學(xué)了,那么再計(jì)算他們的平均分 select
name,avg(score) from stu where name in (select name from (select name,count(*)
as gk from stu having gk >=2) as t) group by name;
3、exists型子查詢 (把外層查詢結(jié)果拿到內(nèi)層,看內(nèi)層的查詢是否成立) #查詢哪些欄目下有商品,欄目表category,商品表goods select
cat_id,cat_name from category where exists(select * from goods where goods.cat_id
= category.cat_id); 三、union的用法
(把兩次或多次的查詢結(jié)果合并起來(lái),要求查詢的列數(shù)一致,推薦查詢的對(duì)應(yīng)的列類型一致,可以查詢多張表,多次查詢語(yǔ)句時(shí)如果列名不一樣,則取第一次的列名!如果不同的語(yǔ)句中取出的行的每個(gè)列的值都一樣,那么結(jié)果將自動(dòng)會(huì)去重復(fù),如果不想去重復(fù)則要加all來(lái)聲明,即union all) ##
現(xiàn)有表a如下
id num
a 5
b 10
c 15
d 10 表b如下
id num
b 5
c 10
d 20
e 99 求兩個(gè)表中id相同的和
select id,sum(num) from (select * from ta union select * from
tb) as tmp group by id;
//以上查詢結(jié)果在本例中的確能正確輸出結(jié)果,但是,如果把tb中的b的值改為10以查詢結(jié)果的b的值就是10了,因?yàn)?/span>ta中的b也是10,所以union后會(huì)被過(guò)濾掉一個(gè)重復(fù)的結(jié)果,這時(shí)就要用union all
select id,sum(num) from (select * from ta union all select
* from tb) as tmp group by id;
#取第4、5欄目的商品,按欄目升序排列,每個(gè)欄目的商品價(jià)格降序排列,用union完成
select goods_id,goods_name,cat_id,shop_price from goods where cat_id=4
union select goods_id,goods_name,cat_id,shop_price from goods where
cat_id=5 order by cat_id,shop_price desc; 【如果子句中有order by 需要用( ) 包起來(lái),但是推薦在最后使用order by,即對(duì)最終合并后的結(jié)果來(lái)排序】 #取第3、4個(gè)欄目,每個(gè)欄目?jī)r(jià)格最高的前3個(gè)商品,結(jié)果按價(jià)格降序排列
(select
goods_id,goods_name,cat_id,shop_price from goods where cat_id=3 order by
shop_price desc limit 3) union (select
goods_id,goods_name,cat_id,shop_price from goods where cat_id=4 order by
shop_price desc limit 3) order by shop_price desc;
四、左連接,右連接,內(nèi)連接
現(xiàn)有表a有10條數(shù)據(jù),表b有8條數(shù)據(jù),那么表a與表b的笛爾卡積是多少? select * from ta,tb
//輸出結(jié)果為8*10=80條 1、左連接 以左表為準(zhǔn),去右表找數(shù)據(jù),如果沒(méi)有匹配的數(shù)據(jù),則以null補(bǔ)空位,所以輸出結(jié)果數(shù)>=左表原數(shù)據(jù)數(shù) 語(yǔ)法:select n1,n2,n3 from ta left
join tb on ta.n1= ta.n2 [這里on后面的表達(dá)式,不一定為=,也可以>,<等算術(shù)、邏輯運(yùn)算符]【連接完成后,可以當(dāng)成一張新表來(lái)看待,運(yùn)用where等查詢】 #取出價(jià)格最高的五個(gè)商品,并顯示商品的分類名稱 select goods_id,goods_name,goods.cat_id,cat_name,shop_price
from goods left join category on goods.cat_id = category.cat_id order by
shop_price desc limit 5; 2、右連接 a left join b 等價(jià)于 b right
join a 推薦使用左連接代替右連接 語(yǔ)法:select
n1,n2,n3 from ta right join tb on ta.n1= ta.n2 3、內(nèi)連接 查詢結(jié)果是左右連接的交集,【即左右連接的結(jié)果去除null項(xiàng)后的并集(去除了重復(fù)項(xiàng))】 mysql目前還不支持 外連接(即左右連接結(jié)果的并集,不去除null項(xiàng)) 語(yǔ)法:select
n1,n2,n3 from ta inner join tb on ta.n1= ta.n2 #########
例:現(xiàn)有表a name
hot
a 12
b 10
c 15
表b:
name hot
d
12
e
10
f
10
g 8
表a左連接表b,查詢hot相同的數(shù)據(jù)
select a.*,b.* from a left join b on a.hot = b.hot
查詢結(jié)果:
name hot name hot
a 12
d 12
b 10
e 10
b 10
f 10
c 15
null null 從上面可以看出,查詢結(jié)果表a的列都存在,表b的數(shù)據(jù)只顯示符合條件的項(xiàng)目 再如表b左連接表a,查詢hot相同的數(shù)據(jù) select a.*,b.* from
b left join a on a.hot = b.hot 查詢結(jié)果為: name hot name
hot
d 12
a 12
e
10 b 10
f
10 b 10
g 8
null null 再如表a右連接表b,查詢hot相同的數(shù)據(jù) select a.*,b.* from a right join b on a.hot = b.hot 查詢結(jié)果和上面的b left join a一樣 ###練習(xí),查詢商品的名稱,所屬分類,所屬品牌 select goods_id,goods_name,goods.cat_id,goods.brand_id,category.cat_name,brand.brand_name
from goods left join category on goods.cat_id = category.cat_id left join brand
on goods.brand_id = brand.brand_id limit 5; 理解:每一次連接之后的結(jié)果都可以看作是一張新表 ###練習(xí),現(xiàn)創(chuàng)建如下表
要求按下面樣式打印2006-0601至 樣式: 火箭 2:0
紅牛
查詢語(yǔ)句為: select
zid,t1.tname as t1name,res,kid,t2.tname as t2name,mtime from m left join t
as t1 on m.zid = t1.tid left join t as t2 on m.kid = t2.tid where
mtime between ' 總結(jié):可以對(duì)同一張表連接多次,以分別取多次數(shù)據(jù)
|
|
|