可以使用集中方法去解決“每個分組中最大的條目”,這類問題已經(jīng)進一步擴展到查詢每組中前N個條目的方法。之后我們深入探討了一些MySQL特定的技術(shù),這些技術(shù)看起來有一些傻和笨。但是如果你需要榨干服務(wù)器的最后一點性能,你就需要知道什么時候去打破規(guī)則。對于那些認為這是MySQL本身的問題的人,我要說這不是,我曾經(jīng)看到過使用其他平臺的人也在做著同樣的事情,如SQL Server。在每個平臺上都會有很多特殊的小技巧和花招,使用他們的人必須去適應(yīng)它。
成都創(chuàng)新互聯(lián)公司是一家以網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、品牌設(shè)計、軟件運維、營銷推廣、小程序App開發(fā)等移動開發(fā)為一體互聯(lián)網(wǎng)公司。已累計為三輪攪拌車等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務(wù)。
一、mysql查詢的五種子句
where(條件查詢)、having(篩選)、group by(分組)、order by(排序)、limit(限制結(jié)果數(shù))
1、where常用運算符:
比較運算符
, ,= , != ( ),= , =
in(v1,v2..vn)
between v1 and v2 在v1至v2之間(包含v1,v2)
邏輯運算符
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 像
通配符:
% 任意字符
_ 單個字符
where goods_name like '諾基亞%'
where goods_name like '諾基亞N__'
2、group by 分組
一般情況下group需與統(tǒng)計函數(shù)(聚合函數(shù))一起使用才有意義
如:select goods_id,goods_name,cat_id,max(shop_price) from goods group by cat_id;
這里取出來的結(jié)果中的good_name是錯誤的!因為shop_price使用了max函數(shù),那么它是取最大的,而語句中使用了group by 分組,那么goods_name并沒有使用聚合函數(shù),它只是cat_id下的第一個商品,并不會因為shop_price改變而改變
mysql中的五種統(tǒng)計函數(shù):
(1)max:求最大值
select max(goods_price) from goods
這里會取出最大的價格的值,只有值
#查詢每個欄目下價格最高的
select cat_id,max(goods_price) from goos group by cat_id;
#查出價格最高的商品編號
select goods_id,max(goods_price) from goods group by goods_id;
(2)min:求最小值
(3)sum:求總數(shù)和
#求商品庫存總和
select sum(goods_number) from goods;
(4)avg:求平均值
#求每個欄目的商品平均價格
select cat_id,avg(goods_price) from goods group by cat_id;
(5)count:求總行數(shù)
#求每個欄目下商品種類
select cat_id,count(*) from goods group by cat_id;
###要把每個字段名當(dāng)成變量來理解,它可以進行運算###
例:查詢本店每個商品價格比市場價低多少;
select goods_id,goods_name,goods_price-market_price from goods;
查詢每個欄目下面積壓的貨款
select cat_id,sum(goods_price*goods_number) from goods group by cat_id;
###可以用as來給計算結(jié)果取個別名###
select cat_id,sum(goods_price * goods_number) as hk from goods group by cat_id
不僅列名可以取別名,表單也可以取別名
3、having 與where 的異同點
having與where類似,可以篩選數(shù)據(jù),where后的表達式怎么寫,having后就怎么寫
where針對表中的列發(fā)揮作用,查詢數(shù)據(jù)
having對查詢結(jié)果中的列發(fā)揮作用,篩選數(shù)據(jù)
#查詢本店商品價格比市場價低多少錢,輸出低200元以上的商品
select goods_id,good_name,market_price - shop_price as s from goods having s200 ;
//這里不能用where因為s是查詢結(jié)果,而where只能對表中的字段名篩選
如果用where的話則是:
select goods_id,goods_name from goods where market_price - shop_price 200;
#同時使用where與having
select cat_id,goods_name,market_price - shop_price as s from goods where cat_id = 3 having s 200;
#查詢積壓貨款超過2萬元的欄目,以及該欄目積壓的貨款
select cat_id,sum(shop_price * goods_number) as t from goods group by cat_id having s 20000
#查詢兩門及兩門以上科目不及格的學(xué)生的平均分
思路:
#先計算所有學(xué)生的平均分
select name,avg(score) as pj from stu group by name;
#查出所有學(xué)生的掛科情況
select name,score60 from stu;
#這里score60是判斷語句,所以結(jié)果為真或假,mysql中真為1假為0
#查出兩門及兩門以上不及格的學(xué)生
select name,sum(score60) as gk from stu group by name having gk 1;
#綜合結(jié)果
select name,sum(score60) as gk,avg(score) as pj from stu group by name having gk 1;
4、order by
(1) order by price //默認升序排列
(2)order by price desc //降序排列
(3)order by price asc //升序排列,與默認一樣
(4)order by rand() //隨機排列,效率不高
#按欄目號升序排列,每個欄目下的商品價格降序排列
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 取出條目
#取價格第4-6高的商品
select good_id,goods_name,goods_price from goods order by good_price desc limit 3,3;
###查詢每個欄目下最貴的商品
思路:
#先對每個欄目下的商品價格排序
select cat_id,goods_id,goods_name,shop_price from goods order by cat_id,shop_price desc;
#上面的查詢結(jié)果中每個欄目的第一行的商品就是最貴的商品
#把上面的查詢結(jié)果理解為一個臨時表[存在于內(nèi)存中]【子查詢】
#再從臨時表中選出每個欄目最貴的商品
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是因為臨時表中每個欄目的第一個商品就是最貴的商品,而group by前面沒有使用聚合函數(shù),所以默認就取每個分組的第一行數(shù)據(jù),這里以cat_id分組
良好的理解模型:
1、where后面的表達式,把表達式放在每一行中,看是否成立
2、字段(列),理解為變量,可以進行運算(算術(shù)運算和邏輯運算)
3、 取出結(jié)果可以理解成一張臨時表
二、mysql子查詢
1、where型子查詢
(把內(nèi)層查詢結(jié)果當(dāng)作外層查詢的比較條件)
#不用order by 來查詢最新的商品
select goods_id,goods_name from goods where goods_id = (select max(goods_id) from goods);
#取出每個欄目下最新的產(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é)的平均成績
思路:
#先查出哪些同學(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é)了,那么再計算他們的平均分
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é)果合并起來,要求查詢的列數(shù)一致,推薦查詢的對應(yīng)的列類型一致,可以查詢多張表,多次查詢語句時如果列名不一樣,則取第一次的列名!如果不同的語句中取出的行的每個列的值都一樣,那么結(jié)果將自動會去重復(fù),如果不想去重復(fù)則要加all來聲明,即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
求兩個表中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了,因為ta中的b也是10,所以union后會被過濾掉一個重復(fù)的結(jié)果,這時就要用union all
select id,sum(num) from (select * from ta union all select * from tb) as tmp group by id;
#取第4、5欄目的商品,按欄目升序排列,每個欄目的商品價格降序排列,用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 需要用( ) 包起來,但是推薦在最后使用order by,即對最終合并后的結(jié)果來排序】
#取第3、4個欄目,每個欄目價格最高的前3個商品,結(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、左連接
以左表為準,去右表找數(shù)據(jù),如果沒有匹配的數(shù)據(jù),則以null補空位,所以輸出結(jié)果數(shù)=左表原數(shù)據(jù)數(shù)
語法:select n1,n2,n3 from ta left join tb on ta.n1= ta.n2 [這里on后面的表達式,不一定為=,也可以,等算術(shù)、邏輯運算符]【連接完成后,可以當(dāng)成一張新表來看待,運用where等查詢】
#取出價格最高的五個商品,并顯示商品的分類名稱
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 等價于 b right join a
推薦使用左連接代替右連接
語法:select n1,n2,n3 from ta right join tb on ta.n1= ta.n2
3、內(nèi)連接
查詢結(jié)果是左右連接的交集,【即左右連接的結(jié)果去除null項后的并集(去除了重復(fù)項)】
mysql目前還不支持 外連接(即左右連接結(jié)果的并集,不去除null項)
語法:select n1,n2,n3 from ta inner join tb on ta.n1= ta.n2
總結(jié):可以對同一張表連接多次,以分別取多次數(shù)據(jù)
select count(表中任意屬性名,如name)
from [table_name];
比如說我有一張state的表,它由姓名年齡構(gòu)成
select count(name)
from state;
1、創(chuàng)建測試表,
create table test_score(name varchar2(20), score number);
2、插入測試數(shù)據(jù)
insert into test_score values('張三', 85);
insert into test_score values('李四', 90);
insert into test_score values('王二', 95);
insert into test_score values('吳七', 83);
insert into test_score values('李六', 66);
commit;
3、查詢表中全量數(shù)據(jù),select t.*, rowid from test_score t;
4、編寫sql,根據(jù)學(xué)生姓名,查詢學(xué)生名詞,以學(xué)生‘李四’為例,成績排名第二;? select t.* from (select t.*, rank() over(order by score desc) rk from test_score t) t where t.name = '李四';
分享文章:mysql怎么查詢第8名 mysql查詢前五名
本文網(wǎng)址:http://chinadenli.net/article10/hhhpdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站收錄、網(wǎng)站排名、網(wǎng)站設(shè)計、網(wǎng)站改版、網(wǎng)站維護
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)