1、mysql查詢的五種子句mysql
where(條件查詢)、having(篩選)、group by(分組)、order by(排序)、limit(限制結果數)sql
一、where經常使用運算符:ide
比較運算符:函數
> , < ,= , != (< >),>= ,<=spa
in(v1,v2..vn) 3d
between v1 and v2 在v1至v2之間(包含v1,v2)regexp
邏輯運算符:blog
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
regexp
通配符:
% 任意字符
_ 單個字符
where goods_name like '諾基亞%'
where goods_name like '諾基亞N__'
二、group by 分組
通常狀況下group需與統計函數(聚合函數)一塊兒使用纔有意義
如,表goods:
select goods_id,goods_name,cat_id,max(shop_price) from goods group by cat_id;
這裏取出來的結果中的goods_id,goods_name是錯誤的!由於shop_price使用了max函數,那麼它是取最大的,而語句中使用了group by 分組,而goods_id,goods_name並無使用聚合函數,它只是cat_id下的第一個商品,並不會由於shop_price改變而改變。
mysql中的五種統計函數:
(1)max:求最大值
select max(shop_price) from goods; 這裏會取出最大的價格的值,只有值
例:查詢每一個欄目下價格最高(最貴)的商品
select * from goods where shop_price in(
select max(shop_price) from goods group by cat_id
); 由於max函數結合group by就能取出分組下的最大值
可是注意,上面不能寫成:
select *,max(shop_price) from goods where shop_price in(select max(shop_price) from goods group by cat_id);
由於只會顯示出價格爲4999的記錄,由於:
上句至關於select *,max(shop_price) from goods where shop_price in(4600,2800,588,4300,3300);
查詢結果爲:
意思是對in中的數據求最大值,並將其全部字段列出來。
而select * from goods where shop_price in(4600,2800,588,4300,3300);查詢結果爲:
注意上面兩句查詢的區別。
還有一種方法,查詢每一個欄目下最貴的商品:
先對每一個欄目下的商品按欄目升序、價格降序
select cat_id,goods_id,goods_name,goods_price from goods order by cat_id,goods_price desc;
上面的查詢結果中每一個欄目的第一行的商品就是最貴的商品,把上面的查詢結果理解爲一個臨時表[存在於內存中]【子查詢】,再從臨時表中選出每一個欄目最貴的商品
select * from (select cat_id,goods_id,goods_name,goods_price from goods order by cat_id,goods_price desc) as t group by cat_id;
這裏使用group by cat_id是由於臨時表中每一個欄目的第一個商品就是最貴的商品,而group by前面沒有使用聚合函數,因此默認就取每一個分組的第一行數據,這裏以cat_id分組。
(2)min:求最小值
(3)sum:求總數和
mysql> select sum(shop_price) from goods;
+-----------------+
| sum(shop_price) |
+-----------------+
| 16383.00 |
+-----------------+
(4)avg:求平均值
mysql> select cat_id,avg(shop_price) from goods group by cat_id;
求每一個欄目的商品平均價格
+--------+-----------------+
| cat_id | avg(shop_price) |
+--------+-----------------+
| 1 | 4499.000000 |
| 2 | 2943.500000 |
| 3 | 549.500000 |
+--------+-----------------+
(5)count:求總行數
mysql> select cat_id,count(*) from goods group by cat_id;
求每一個欄目下商品種類
+--------+----------+
| cat_id | count(*) |
+--------+----------+
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
+--------+----------+
要把每一個字段名當成變量來理解,它能夠進行運算
例:查詢本店每一個商品價格比市場價低多少;
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來給計算結果取個別名
mysql> select cat_id,sum(shop_price * cat_id) as hk from goods group by cat_id;
+--------+----------+
| cat_id | hk |
+--------+----------+
| 1 | 8998.00 |
| 2 | 11774.00 |
| 3 | 3297.00 |
+--------+----------+
不只列名能夠取別名,表單也能夠取別名
三、having 與where 的異同點
where針對表中的列發揮做用,查詢數據
having對查詢結果中的列發揮做用,篩選數據
例:查詢本店商品價格比市場價低多少錢,輸出低200元以上的商品
select goods_id,goods_name,market_price - goods_price as s from goods having s>200 ;
這裏不能用where由於s是查詢結果,而where只能對錶中的字段名篩選,若是用where的話則是:
select goods_id,goods_name from goods where market_price - goods_price > 200;
同時使用where與having:
select cat_id,goods_name,market_price - goods_price as s from goods where cat_id = 2 having s > 500;
例:查詢積壓貨款超過2萬元的欄目,以及該欄目積壓的貨款
mysql> select cat_id,sum(goods_price * goods_num) as s from goods group by cat_id having s > 20000;
+--------+----------+
| cat_id | s |
+--------+----------+
| 1 | 57500.00 |
| 2 | 29600.00 |
+--------+----------+
例:查詢兩門及兩門以上科目不及格的學生的平均分
先建表、添加數據
mysql> select * from stuscore;
+----+------+---------+-------+
| id | name | subject | score |
+----+------+---------+-------+
| 1 | 張靜 | 語文 | 45 |
| 2 | 張靜 | 數學 | 67 |
| 3 | 張靜 | 英語 | 87 |
| 4 | 張靜 | 物理 | 76 |
| 5 | 張靜 | 化學 | 87 |
| 6 | 王壯 | 語文 | 67 |
| 7 | 王壯 | 數學 | 89 |
| 8 | 王壯 | 英語 | 78 |
| 9 | 王壯 | 物理 | 98 |
| 10 | 王壯 | 化學 | 87 |
| 11 | 劉都 | 語文 | 88 |
| 12 | 劉都 | 數學 | 43 |
| 13 | 劉都 | 英語 | 56 |
| 14 | 劉都 | 物理 | 43 |
| 15 | 劉都 | 化學 | 57 |
| 16 | 周靈 | 語文 | 59 |
| 17 | 周靈 | 數學 | 54 |
| 18 | 周靈 | 英語 | 98 |
| 19 | 周靈 | 物理 | 60 |
| 20 | 周靈 | 化學 | 69 |
| 21 | 李歌 | 語文 | 90 |
| 22 | 李歌 | 數學 | 73 |
| 23 | 李歌 | 英語 | 59 |
| 24 | 李歌 | 物理 | 71 |
| 25 | 李歌 | 化學 | 69 |
+----+------+---------+-------+
-- 每一個學生的平均分
mysql> select name,avg(score) as avgScore from stuscore group by name;
+------+----------+
| name | avgScore |
+------+----------+
| 劉都 | 57.4 |
| 周靈 | 68 |
| 張靜 | 72.4 |
| 李歌 | 72.4 |
| 王壯 | 83.8 |
+------+----------+
-- 查出全部學生的掛科狀況
mysql> select name,score<60 from stuscore;
+------+----------+
| name | score<60 |
+------+----------+
| 張靜 | 1 |
| 張靜 | 0 |
| 張靜 | 0 |
| 張靜 | 0 |
| 張靜 | 0 |
| 王壯 | 0 |
| 王壯 | 0 |
| 王壯 | 0 |
| 王壯 | 0 |
| 王壯 | 0 |
| 劉都 | 0 |
| 劉都 | 1 |
| 劉都 | 1 |
| 劉都 | 1 |
| 劉都 | 1 |
| 周靈 | 1 |
| 周靈 | 1 |
| 周靈 | 0 |
| 周靈 | 0 |
| 周靈 | 0 |
| 李歌 | 0 |
| 李歌 | 0 |
| 李歌 | 1 |
| 李歌 | 0 |
| 李歌 | 0 |
+------+----------+
-- 查出兩門及兩門以上不及格的學生
mysql> select name,sum(score<60) as gk from stuscore group by name having gk > 1;
+------+------+
| name | gk |
+------+------+
| 劉都 | 4 |
| 周靈 | 2 |
+------+------+
-- 綜合結果
mysql> select name,sum(score<60) as gk,avg(score) as pj from stuscore group by name having gk >1;
+------+------+------+
| name | gk | pj |
+------+------+------+
| 劉都 | 4 | 57.4 |
| 周靈 | 2 | 68 |
+------+------+------+
四、order by
(1)order by price,order by price asc //默認升序排列
(2)order by price desc //降序排列
(3)order by rand() //隨機排列,效率不高
例:按欄目號升序排列,每一個欄目下的商品價格降序排列
select * from goods where cat_id !=2 order by cat_id,goods_price desc;
五、limit
limit [offset,] N
offset 偏移量,可選,不寫則至關於limit 0,N
N 取出條目
例:取價格第4-6條的商品
select goods_id,goods_name,goods_price from goods order by goods_price desc;
select goods_id,goods_name,goods_price from goods order by goods_price desc limit 3,3;
良好的理解模型:
一、where後面的表達式,把表達式放在每一行中,看是否成立
二、字段(列),理解爲變量,能夠進行運算(算術運算和邏輯運算)
三、 取出結果能夠理解成一張臨時表
2、mysql子查詢
一、where型子查詢(把內層查詢結果看成外層查詢的比較條件)
例:查詢最新的商品mysql> select goods_id,goods_name from goods where goods_id = (select max(goods_id) from goods);
+----------+------------+
| goods_id | goods_name |
+----------+------------+
| 6 | 固態硬盤 |
+----------+------------+
例:查詢每一個欄目下最新的產品(goods_id惟一)
mysql> select cat_id,goods_id,goods_name from goods where goods_id in(select max(goods_id) from goods group by cat_id);
+--------+----------+-------------+
| cat_id | goods_id | goods_name |
+--------+----------+-------------+
| 1 | 2 | Dell筆記本 |
| 2 | 4 | 臺式機-華碩 |
| 3 | 6 | 固態硬盤 |
+--------+----------+-------------+
二、from型子查詢(把內層的查詢結果供外層再次查詢)
例:用子查詢查出掛科兩門及以上的同窗的平均成績
-- 先查出哪些同窗掛科兩門以上
mysql> select name,count(*) as gk from stuscore where score < 60 group by name having gk >=2; -- group by語句不能放在where語句前面
+------+----+
| name | gk |
+------+----+
| 劉都 | 4 |
| 周靈 | 2 |
+------+----+
-- 以上查詢結果,咱們只要名字就能夠了,因此再取一次名字
mysql> select name from (select name,count(*) as gk from stuscore where score < 60 group by name having gk >=2) as t;
+------+
| name |
+------+
| 劉都 |
| 周靈 |
+------+
-- 找出這些同窗了,那麼再計算他們的平均分
mysql> select name,avg(score) from stuscore where name in (
select name from (select name,count(*) as gk from stuscore where score < 60
group by name having gk >=2) as t
) group by name;
+------+------------+
| name | avg(score) |
+------+------------+
| 劉都 | 57.4 |
| 周靈 | 68 |
+------+------------+
三、exists型子查詢(把外層查詢結果拿到內層,看內層的查詢是否成立)
例:查詢哪些欄目下有商品,欄目表category,商品表goods
mysql> select cat_id,cat_name from category where exists(select * from goods where goods.cat_id = category.cat_id);
+--------+----------+
| cat_id | cat_name |
+--------+----------+
| 1 | 筆記本 |
| 2 | 臺式機 |
| 3 | 硬盤 |
+--------+----------+
3、union的用法
把兩次或屢次的查詢結果合併起來,要求查詢的列數一致,推薦查詢的對應的列類型一致,能夠查詢多張表,屢次查詢語句時若是列名不同,則取第一次的列名!若是不一樣的語句中取出的行的每一個列的值都同樣,那麼結果將自動會去重複,若是不想去重複則要加all來聲明,即union all。
現有表a以下:
id num
a 5
b 10
c 15
d 10
表b以下:
id num
b 5
c 10
d 20
e 99
例:求兩個表中id相同的和
mysql> select * from a union select * from b;
+----+------+
| id | num |
+----+------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
| b | 5 |
| c | 10 |
| d | 20 |
| e | 99 |
+----+------+
mysql> select id,sum(num) from (select * from a union select * from b) as tmp group by id;
+----+----------+
| id | sum(num) |
+----+----------+
| a | 5 |
| b | 15 |
| c | 25 |
| d | 30 |
| e | 99 |
+----+----------+
以上查詢結果在本例中的確能正確輸出結果,可是,若是把表b中的b的值改成10後查詢結果的b的值就是10了,由於表a中的b也是10,因此union後會被過濾掉一個重複的結果,這時就要用union all
mysql> select * from a union all select * from b;
+----+------+
| id | num |
+----+------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
| b | 10 |
| c | 10 |
| d | 20 |
| e | 99 |
+----+------+
mysql> select id,sum(num) from (select * from a union all select * from b)
as tmp group by id;
+----+----------+
| id | sum(num) |
+----+----------+
| a | 5 |
| b | 20 |
| c | 25 |
| d | 30 |
| e | 99 |
+----+----------+
例:取第四、5欄目的商品,按欄目升序排列,每一個欄目的商品價格降序排列,用union完成
mysql> select goods_id,goods_name,cat_id,goods_price from goods where cat_id=4 union
select goods_id,goods_name,cat_id,goods_price from goods where cat_id=5
order by cat_id,goods_price desc;
+----------+----------------+--------+-------------+
| goods_id | goods_name | cat_id | goods_price |
+----------+----------------+--------+-------------+
| 7 | 諾基亞1520 | 4 | 4000.00 |
| 8 | 諾基亞920 | 4 | 2400.00 |
| 9 | 蘋果 iPad | 5 | 3300.00 |
| 10 | 微軟 Surface 2 | 5 | 3100.00 |
+----------+----------------+--------+-------------+
若是不用union,則更簡單:
select goods_id,goods_name,cat_id,goods_price from goods where cat_id=4 or cat_id=5 order by cat_id,goods_price desc;
【若是子句中有order by 須要用( ) 包起來,可是推薦在最後使用order by,即對最終合併後的結果來排序】
例:取第三、4個欄目,每一個欄目價格最高的前3個商品,結果按價格降序排列
mysql> (select goods_id,goods_name,cat_id,goods_price from goods where cat_id=3 order by goods_price desc limit 3) union
-> (select goods_id,goods_name,cat_id,goods_price from goods where cat_id=4 order by goods_price desc limit 3)
-> order by goods_price desc;
+----------+------------+--------+-------------+
| goods_id | goods_name | cat_id | goods_price |
+----------+------------+--------+-------------+
| 14 | 蘋果5 | 4 | 4300.00 |
| 7 | 諾基亞1520 | 4 | 4000.00 |
| 13 | 蘋果4S | 4 | 3000.00 |
| 6 | 固態硬盤 | 3 | 588.00 |
| 5 | 移動硬盤 | 3 | 320.00 |
| 12 | U盤-16G | 3 | 70.00 |
+----------+------------+--------+-------------+
4、左鏈接,右鏈接,內鏈接
現有表a有4條數據,表b有4條數據,那麼表a與表b的笛爾卡積是多少?
mysql> select * from a,b; 輸出結果爲4*4=16條
+----+------+----+------+
| id | num | id | num |
+----+------+----+------+
| a | 5 | b | 10 |
| b | 10 | b | 10 |
| c | 15 | b | 10 |
| d | 10 | b | 10 |
| a | 5 | c | 10 |
| b | 10 | c | 10 |
| c | 15 | c | 10 |
| d | 10 | c | 10 |
| a | 5 | d | 20 |
| b | 10 | d | 20 |
| c | 15 | d | 20 |
| d | 10 | d | 20 |
| a | 5 | e | 99 |
| b | 10 | e | 99 |
| c | 15 | e | 99 |
| d | 10 | e | 99 |
+----+------+----+------+
一、左鏈接
以左表爲準,去右表找數據,若是沒有匹配的數據,則以null補空位,因此輸出結果數>=左表原數據數
語法:select n1,n2,n3 from ta left join tb on a.n1= b.n2 [這裏on後面的表達式,不必定爲=,也能夠>,<等算術、邏輯運算符]【鏈接完成後,能夠當成一張新表來看待,運用where等查詢】
例:取出價格最高的五個商品,並顯示商品的分類名稱
如今的goods表以下,已將goods_price按降序排列:
mysql> select goods_id,goods_name,goods.cat_id,cat_name,goods_price from goods left join category
-> on goods.cat_id = category.cat_id order by goods_price desc limit 5;
+----------+------------+--------+----------+-------------+
| goods_id | goods_name | cat_id | cat_name | goods_price |
+----------+------------+--------+----------+-------------+
| 2 | Dell筆記本 | 1 | 筆記本 | 4600.00 |
| 14 | 蘋果5 | 4 | 手機 | 4300.00 |
| 7 | 諾基亞1520 | 4 | 手機 | 4000.00 |
| 1 | 聯想筆記本 | 1 | 筆記本 | 3450.00 |
| 9 | 蘋果 iPad | 5 | 平板電腦 | 3300.00 |
+----------+------------+--------+----------+-------------+
二、右鏈接
a left join b 等價於 b right join a
推薦使用左鏈接代替右鏈接
語法:select n1,n2,n3 from a right join b on a.n1= b.n2
三、內鏈接
查詢結果是左右鏈接的交集,【即左右鏈接的結果去除null項後的並集(去除了重複項)】
mysql目前還不支持外鏈接(即左右鏈接結果的並集,不去除null項)
語法:select n1,n2,n3 from a inner join b on a.n1= b.n2
例:現有表a
+----+------+
| id | num |
+----+------+
| a | 12 |
| b | 10 |
| c | 15 |
+----+------+
表b:
+----+------+
| id | num |
+----+------+
| d | 12 |
| e | 10 |
| f | 10 |
| g | 8 |
+----+------+
例:表a左鏈接表b,查詢num相同的數據
mysql> select a.*,b.* from a left join b on a.num = b.num;
+----+------+------+------+
| id | num | id | num |
+----+------+------+------+
| a | 12 | d | 12 |
| b | 10 | e | 10 |
| b | 10 | f | 10 |
| c | 15 | NULL | NULL |
+----+------+------+------+
從上面能夠看出,查詢結果表a的列都存在,表b的數據只顯示符合條件的項目
例:表b左鏈接表a,查詢num相同的數據;a右鏈接表b,查詢num相同的數據----這兩種查詢結果相同:
mysql> select a.*,b.* from b left join a on a.num = b.num;
mysql> select a.*,b.* from a right join b on a.num = b.num;
+------+------+----+------+
| id | num | id | num |
+------+------+----+------+
| a | 12 | d | 12 |
| b | 10 | e | 10 |
| b | 10 | f | 10 |
| NULL | NULL | g | 8 |
+------+------+----+------+
例:查詢商品的名稱,所屬分類,所屬品牌
先查看更改後的表結構:
表goods:
表category:
表brand:
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;
理解:每一次鏈接以後的結果均可以看做是一張新表
例:建立兩張表,表m記錄比賽信息,表t記錄球隊信息
要求查詢出2006-0601至2006-07-01期間的比賽結果:
select m.*,t.* from m left join t on m.zid = t.tid;
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;
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';
總結:能夠對同一張錶鏈接屢次,以分別取屢次數據