查詢
select 列1,列2,列3 from 表 where 條件
select查詢模型
列是變量
條件可以了解為表達式
比如:
select goods_id,goods_name,shop_price,shop_market_price,shopmarketprice - shopprice from goods where shopmarketprice - shopprice >10
條件查詢
我們查詢的時候可以加上where條件這樣既能取得我們想要的資料

這裡做一個簡單查詢練習
查出滿足以下條件的商品
1.1:主鍵為32的商品
select goods_id,goods_name,shop_price from ecs_goods where goods_id=32;
2.1.2:不屬第3欄目的所有商品
select goods_id,cat_id,goods_name,shop_price from ecs_goods where cat_id!=3;
2.1.3:本店價格高于3000元的商品
select goods_id,cat_id,goods_name,shop_price from ecs_goods where shop_price >3000;
2.1.4:本店價格低于或等于100元的商品
select goods_id,cat_id,goods_name,shop_price from ecs_goods where shop_price <=100;
2.1.5:取出第4欄目或第11欄目的商品(不許用or)
select goods_id,cat_id,goods_name,shop_price from ecs_goods
where cat_id in (4,11);
2.1.6:取出100<=價格<=500的商品(不許用and)
select goods_id,cat_id,goods_name,shop_price from ecs_goods
where shop_price between 100 and 500;
2.1.7:取出不屬于第3欄目且不屬于第11欄目的商品(and,或not in分别實作)
select goods_id,cat_id,goods_name,shop_price from ecs_goods where cat_id!=3 and cat_id!=11;
select goods_id,cat_id,goods_name,shop_price from ecs_goods where cat_id not in (3,11);
2.1.8:取出價格大于100且小于300,或者大于4000且小于5000的商品(and的優先級高于or)
select goods_id,cat_id,goods_name,shop_price from ecs_goods where shop_price>100 and shop_price <300 or shop_price >4000 and shop_price <5000;
2.1.9:取出第3個欄目下面價格<1000或>3000,并且點選量>5的系列商品(注意and的優先級高于or)
select goods_id,cat_id,goods_name,shop_price,click_count from ecs_goods where
cat_id=3 and (shop_price <1000 or shop_price>3000) and click_count>5;
2.1.10:取出第1個欄目下面的商品(注意:1欄目下面沒商品,但其子欄目下有)
select goods_id,cat_id,goods_name,shop_price,click_count from ecs_goods
where cat_id in (2,3,4,5);
2.1.11:取出名字以"諾基亞"開頭的商品
select goods_id,cat_id,goods_name,shop_price from ecs_goods where goods_name like '諾基亞%';
2.1.12:取出名字為"諾基亞Nxx"的手機
where goods_name like '諾基亞N__';
2.1.13:取出名字不以"諾基亞"開頭的商品
select goods_id,cat_id,goods_name,shop_price from ecs_goos
where goods_name not like '諾基亞%';
2.1.14:取出第3個欄目下面價格在1000到3000之間,并且點選量>5 "諾基亞"開頭的系列商品
select goods_id,cat_id,goods_name,shop_price from ecs_goods where
cat_id=3 and shop_price>1000 and shop_price <3000 and click_count>5 and goods_name like '諾基亞%';
shop_price between 1000 and 3000 and cat_id=3 and click_count>5 and goods_name like '諾基亞%';
2.1.15把num值處于[20,29]之間,改為20,num值處于[30,39]之間的,改為30
update table set num=floor(num/10)*10 where num >=20 and num <=39
2.1.16把good表中商品名為'諾基亞xxxx'的商品,改為'HTCxxxx',
update goods set goods_name=concat("HTC",substring(goods_name,4)) where goods_name like "諾基亞%"
奇怪的null
當我們的某個字段設定為可以為空,我們插入資料時可以插入null。
select from 表 where 字段 != null 這樣是查詢不到任何資料的
select from 表 where 字段 = null 這樣是查詢不到任何資料的
注意在mysql資料庫中null是不等于null
當我們要想查字段是否是null專門提供一個查詢條件 is null 如下
select * from 表 where 字段 is null 這樣能查出來字段為null的值
這種查詢方式不利于優化,是以不推薦使用,我們建立字段是可以設定成不為空再加上一個預設值,預設值為‘’空字元串。
聚合函數
聚合函數(常用于group by從句的select查詢中)
avg(col)傳回指定列的平均值
count(col)傳回指定列中非null值的個數
min(col)傳回指定列的最小值
max(col)傳回指定列的最大值
sum(col)傳回指定列的所有值之和
group_concat(col) 傳回由屬于一組的列值連接配接組合而成的結果
2.1:查出最貴的商品的價格
select max(shop_price) from ecs_goods;
2.2:查出最大(最新)的商品編号
select max(goods_id) from ecs_goods;
2.3:查出最便宜的商品的價格
select min(shop_price) from ecs_goods;
2.4:查出最舊(最小)的商品編号
select min(goods_id) from ecs_goods;
2.5:查詢該店所有商品的庫存總量
select sum(goods_number) from ecs_goods;
2.6:查詢所有商品的平均價
select avg(shop_price) from ecs_goods;
2.7:查詢該店一共有多少種商品
select count(*) from ecs_goods;
分組查詢
select 聚合函數,分組字段,【其他字段】 from 表 group by 分組字段
group by 時會mysql内部會先根據分組字段排序然後再統計,我們可以給分組字段設定索引。
我們分組查詢時我們查找的字段有聚合函數,分組字段,這都是沒有問題的,但是有個其他字段是有問題的,這個其他字段到底是那條記錄的值呢?就好比我讓大家按宿舍統計宿舍人的平均身高,這時我們查詢的宿舍号和平均身高都沒有問題,但是你又要了姓名,這時該用誰的姓名呢?其實這種寫法語義上是有問題的,在orancle上直接會報錯,但是mysql上不會報錯,這時這個其他字段會使用第一次出現的那一行的那個列來填充。
查詢每個欄目下面最貴商品價格
select cat_id,max(shop_price) from ecs_goods group by cat_id;
group_concat的使用
having篩選
我們要想查詢市場價比本店高超過200個商品
select goods_id,goods_name,market_price-shop_price
from ecs_goods where market_price-shop_price > 200 ;
上面的寫法會計算市場價和本店價內插補點計算2次
我們可以使用一個别名的方式
select goods_id,goods_name,market_price-shop_price as j
from ecs_goods where j > 200 ;
上面的寫法會報錯,說不認識 j 這個字段。
這時我們不能使用where了我們得使用having
from ecs_goods having j > 200 ;
where和having的差別
where是針對磁盤上的資料進行篩選,篩選出結果集後放在記憶體中我們再使用having進行篩選。having永遠不能放在where前面。
分組查詢練習
3 having與group綜合運用查詢:
3.1:查詢該店的商品比市場價所節省的價格
select goods_id,goods_name,market_price-shop_price as j
from ecs_goods ;
3.2:查詢每個商品所積壓的貨款(提示:庫存單價)
select goods_id,goods_name,goods_numbershop_price from ecs_goods
3.3:查詢該店積壓的總貨款
select sum(goods_number*shop_price) from ecs_goods;
3.4:查詢該店每個欄目下面積壓的貨款.
select cat_id,sum(goods_number*shop_price) as k from ecs_goods group by cat_id;
3.5:查詢比市場價省錢200元以上的商品及該商品所省的錢(where和having分别實作)
select goods_id,goods_name,market_price-shop_price as k from ecs_goods
where market_price-shop_price >200;
having k >200;
3.6:查詢積壓貨款超過2W元的欄目,以及該欄目積壓的貨款
select cat_id,sum(goods_number*shop_price) as k from ecs_goods group by cat_id having k>20000
排序order by
order by 字段 不寫就是正序
order by 字段 asc 正序
order by 字段 desc 倒叙
order by 字段1 正序,字段2 desc 先按照字段1進行正序排,如果字段1都一樣的話,再根據字段2進行倒叙排列。
限制條數limit
limit 3 取前3條
limit 5,3 從第5條取,取3條。
select 字段1,字段2 ,聚合函數 from 表 where 條件 group by 字段 having 條件 order by 字段 limit 便宜量,限制條數
有如下表及資料
+------+---------+-------+
| name | subject | score |
| 張三 | 數學 | 90 |
| 張三 | 國文 | 50 |
| 張三 | 地理 | 40 |
| 李四 | 國文 | 55 |
| 李四 | 政治 | 45 |
| 王五 | 政治 | 30 |
要求:查詢出2門及2門以上不及格者的平均成績
一種錯誤做法
mysql> select name,count(score<60) as k,avg(score) from stu group by name having k>=2;
+------+---+------------+
| name | k | avg(score) |
| 張三 | 3 | 60.0000 |
| 李四 | 2 | 50.0000 |
2 rows in set (0.00 sec)
#利用having篩選挂科2門以上的.
mysql> select name,sum(score < 60) as gk ,avg(score) as pj from stu group by name having gk >=2;
+------+------+---------+
| name | gk | pj |
| 張三 | 2 | 60.0000 |
| 李四 | 2 | 50.0000 |
where 型子查詢
将查詢的結果作為另一條查詢語句的查詢條件
查詢goods_id最大的那條資料
select from goods where goods_id=(select max(goods_id) from goods);
查詢每個分類下goods_id最大的那個商品的信心
select from goods where goods_id in (select max(goods_id) from goods group by cate_id);
from型子查詢
将查詢的結果集作為另一條查詢語句的表來看
select from (select from goods order by cate_id asc,goods_id desc) group by cate_id
exists型的子查詢
查出欄目下沒有商品的欄目
select from cate where exists(select from goods where
goods.cate_id=cate.cate_id;
内連接配接
select from goods inner join cate on goods.cate_id = cate.cate_id
inner 可以省略
如果goods表中的資料在cate表中比對不到資料時,就把goods表條這資料就丢掉。
左(外)連接配接
select from goods left join cate on goods.cate_id = cate.cate_id
如果goods表中的資料在cate表中比對不到資料時,這是會把goods表中的資料留下來,cate表的字段用null進行填充
右(外)連接配接
select from goods right join cate on goods.cate_id = cate.cate_id
如果goods表中的資料在cate表中比對不到資料時,會把cate表中的資料留下來,goods表的字段用null進行填充。
全(外)連接配接
select from goods full join cate on goods.cate_id = cate.cate_id
如果goods表中的資料在cate表中比對不到資料時,會把goods表中的資料留下來,cate表的字段用null進行填充。如果cate表中的資料在goods表中比對不到資料時,會把cate表中的資料留下來,goods表的字段用null進行填充。
交叉查詢
select from 表1 cross join 表2
獲得表1和表2的笛卡爾積
簡寫形式 select from 表1,表2
聯合查詢
将查詢的結果集縱向的合并為一個結果集
select goods_id,goods_name from goods where cate_i=2
unnin
select goods_id,goods_name from goods where cate_id=4
資料源可以是同一張表,亦可以是不同表,各個語句取出的列數必須相同,字段名稱用的是第一個sql語句的字段。注意上面的資料有完全相同的會被合并(去重),合并和花費大量的時間
我們可以使用 union all 這樣就不會将相同的資料合并(不會去重)
unnin all
union聯合查詢時每個sql語句排序是沒有意義的。