一缴川、mysql查詢(xún)的五種子句
where(條件查詢(xún))、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之間的值
模糊查詢(xún)
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)閟hop_price使用了max函數(shù)嘹狞,那么它是取最大的岂膳,而語(yǔ)句中使用了group by 分組,那么goods_name并沒(méi)有使用聚合函數(shù)磅网,它只是cat_id下的第一個(gè)商品谈截,并不會(huì)因?yàn)閟hop_price改變而改變
mysql中的五種統(tǒng)計(jì)函數(shù):
(1)max:求最大值
select max(goods_price) from goods
這里會(huì)取出最大的價(jià)格的值,只有值
#查詢(xún)每個(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è)欄目下商品種類(lèi)
select cat_id,count(*) from goods group by cat_id;
###要把每個(gè)字段名當(dāng)成變量來(lái)理解涧偷,它可以進(jìn)行運(yùn)算###
例:查詢(xún)本店每個(gè)商品價(jià)格比市場(chǎng)價(jià)低多少簸喂;
select goods_id,goods_name,goods_price-market_price from goods;
查詢(xún)每個(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類(lèi)似喻鳄,可以篩選數(shù)據(jù),where后的表達(dá)式怎么寫(xiě)确封,having后就怎么寫(xiě)
where針對(duì)表中的列發(fā)揮作用诽表,查詢(xún)數(shù)據(jù)
having對(duì)查詢(xún)結(jié)果中的列發(fā)揮作用,篩選數(shù)據(jù)
#查詢(xún)本店商品價(jià)格比市場(chǎng)價(jià)低多少錢(qián)隅肥,輸出低200元以上的商品
select goods_id,good_name,market_price - shop_price as s from goods having s>200 ;
//這里不能用where因?yàn)閟是查詢(xún)結(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;
#查詢(xún)積壓貨款超過(guò)2萬(wàn)元的欄目,以及該欄目積壓的貨款
select cat_id,sum(shop_price * goods_number) as t from goods group by cat_id having s > 20000
#查詢(xún)兩門(mén)及兩門(mén)以上科目不及格的學(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
#查出兩門(mén)及兩門(mén)以上不及格的學(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 偏移量岗仑,可選匹耕,不寫(xiě)則相當(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;
###查詢(xún)每個(gè)欄目下最貴的商品
思路:
#先對(duì)每個(gè)欄目下的商品價(jià)格排序
select cat_id,goods_id,goods_name,shop_price from goods order by cat_id,shop_price desc;
#上面的查詢(xún)結(jié)果中每個(gè)欄目的第一行的商品就是最貴的商品
#把上面的查詢(xún)結(jié)果理解為一個(gè)臨時(shí)表[存在于內(nèi)存中]【子查詢(xún)】
#再?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子查詢(xún)
1洒敏、where型子查詢(xún)
(把內(nèi)層查詢(xún)結(jié)果當(dāng)作外層查詢(xún)的比較條件)
#不用order by 來(lái)查詢(xún)最新的商品
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型子查詢(xún)
(把內(nèi)層的查詢(xún)結(jié)果供外層再次查詢(xún))
#用子查詢(xún)查出掛科兩門(mén)及以上的同學(xué)的平均成績(jī)
思路:
#先查出哪些同學(xué)掛科兩門(mén)以上
select name,count(*) as gk from stu where score < 60 having gk >=2;
#以上查詢(xún)結(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型子查詢(xún)
(把外層查詢(xún)結(jié)果拿到內(nèi)層镊靴,看內(nèi)層的查詢(xún)是否成立)
#查詢(xún)哪些欄目下有商品,欄目表category,商品表goods
select cat_id,cat_name from category where exists(select * from goods where goods.cat_id = category.cat_id);
三链韭、union的用法
(把兩次或多次的查詢(xún)結(jié)果合并起來(lái)偏竟,要求查詢(xún)的列數(shù)一致,推薦查詢(xún)的對(duì)應(yīng)的列類(lèi)型一致敞峭,可以查詢(xún)多張表踊谋,多次查詢(xún)語(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 taunionselect * from tb) as tmp group by id;
//以上查詢(xún)結(jié)果在本例中的確能正確輸出結(jié)果睦疫,但是,如果把tb中的b的值改為10以查詢(xún)結(jié)果的b的值就是10了鞭呕,因?yàn)閠a中的b也是10蛤育,所以u(píng)nion后會(huì)被過(guò)濾掉一個(gè)重復(fù)的結(jié)果,這時(shí)就要用union all
select id,sum(num) from (select * from taunion allselect * 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 taleft jointbonta.n1= ta.n2 [這里on后面的表達(dá)式,不一定為=辟狈,也可以>肠缔,<等算術(shù)、邏輯運(yùn)算符]【連接完成后哼转,可以當(dāng)成一張新表來(lái)看待明未,運(yùn)用where等查詢(xún)】
#取出價(jià)格最高的五個(gè)商品,并顯示商品的分類(lèi)名稱(chēng)
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 taright jointbonta.n1= ta.n2
3趟妥、內(nèi)連接
查詢(xún)結(jié)果是左右連接的交集,【即左右連接的結(jié)果去除null項(xiàng)后的并集(去除了重復(fù)項(xiàng))】
mysql目前還不支持 外連接(即左右連接結(jié)果的并集,不去除null項(xiàng))
語(yǔ)法:select n1,n2,n3 from tainner jointbonta.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佣蓉,查詢(xún)hot相同的數(shù)據(jù)
select a.*,b.* from aleft joinb on a.hot = b.hot
查詢(xún)結(jié)果:
name ?hot ? name ?hot
a ? ? ? 12 ? ? d ? ? ? 12
b ? ? ? 10 ? ? e ? ? ? 10
b ? ? ? 10 ? ? f ? ? ? ?10
c ? ? ? 15 ? ? null ? ?null
從上面可以看出披摄,查詢(xún)結(jié)果表a的列都存在,表b的數(shù)據(jù)只顯示符合條件的項(xiàng)目
再如表b左連接表a勇凭,查詢(xún)hot相同的數(shù)據(jù)
select a.*,b.* from bleft joina on a.hot = b.hot
查詢(xún)結(jié)果為:
name ?hot ? name ?hot
d ? ? ? 12 ? ? a ? ? ? 12
e ? ? ? ?10 ? ?b ? ? ? 10
f ? ? ? ?10 ? ? b ? ? ?10
g ? ? ? ?8 ? ? null ? ?null
再如表a右連接表b疚膊,查詢(xún)hot相同的數(shù)據(jù)
select a.*,b.* from aright joinb on a.hot = b.hot
查詢(xún)結(jié)果和上面的b left join a一樣
###練習(xí),查詢(xún)商品的名稱(chēng)虾标,所屬分類(lèi)寓盗,所屬品牌
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)建如下表
createtablem(
idint,
zidint,
kidint,
resvarchar(10),
mtimedate
) charset utf8;
insertintomvalues
(1,1,2,'2:0','2006-05-21'),
(2,3,2,'2:1','2006-06-21'),
(3,1,3,'2:2','2006-06-11'),
(4,2,1,'2:4','2006-07-01');
createtablet
(tidint,tnamevarchar(10)) charset utf8;
insertintotvalues
(1,'申花'),
(2,'紅牛'),
(3,'火箭');
要求按下面樣式打印2006-0601至2006-07-01期間的比賽結(jié)果
樣式:
火箭 ? 2:0 ? ?紅牛 ?2006-06-11
查詢(xún)語(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 '2006-06-01' and '2006-07-01';
總結(jié):可以對(duì)同一張表連接多次,以分別取多次數(shù)據(jù)
轉(zhuǎn)載:http://www.cnblogs.com/rollenholt/archive/2012/05/15/2502551.html