select
? ? 作用:獲取MySQL中的數據行
單獨使用select:
? ? select @@xxx? 查詢參數信息
? ? ? ? ? ? ? mysql> select @@port;
? mysql> show variables like '%innodb%';
? ? select 函數? mysql> select database(); 查看當前所在的庫
? ? ? ? ? ? ? ? ? ? ? mysql> select now();? ? ? 查看當前時間
? ? ? ? ? ? ? ? ? ? ? mysql> select version();? 查看當前數據庫版本
? ? select語法執行順序
select開始:至少要配合一個from
? ? ? ? ? ? ↓ 例子:1.use 數據庫名;? 2.select *from city;適合表數據行比較少的? 3.
from自句
? ? ? ? ? ? ↓
where子句
? ? ? ? ? ? ↓
group by子句
? ? ? ? ? ? ↓
select后執行條件
? ? ? ? ? ? ↓
having子句
? ? ? ? ? ? ↓
order by
? ? ? ? ? ? ↓
limit
? select開始:至少要配合一個from
? ? ? ? ? ? ↓ 例子1:1.use 數據庫名;?
? ? ? ? ? ? ? ? ? ? ? ? 2.select *from city;適合表數據行比較少的?
? ? ? 例子2: 查詢name列 和 population列所有的值
? ? ? select name,population from world.city;
? where子句:
? ? ? ? ? ? 例子1:where 配合 等值查詢
? 查詢city 表中 中國城市信息
? select * from world.city where countrycode='CHN';? 從 world庫的city表中? 查詢 關于 CHN 的
查詢city 表中 美國城市信息 ?
? select * from world.city where countrycode='USA';? 從 world庫的city表中? 查詢 關于 USA 的
? ? ? ? ? ? 例子2:where 配合 不等值查詢< >? <=? >=? <>
查詢世界人口小于100人的城市
select *? from? world.city? where population<100;? 從 world庫的city表中? 查詢 關于 人口<100 的
查詢世界人口大于10000000人的城市
select *? from? world.city? where population>10000000;? 從 world庫的city表中? 查詢 關于 人口>10000000 的
? ? ? ? ? ? 例子2:where 配合 模糊查詢(like)
? ? 查詢國家代號是C的城市
select *? from? world.city? where countrycode like 'C%';
注意!!! like語句中 不要出現%在前面的情況 因為效率很低 不走索引
? ? ? ? ? ? 例子3:where 配合 邏輯連接符查詢(AND? OR)
? ? 查詢城市人口1萬 到 2萬之間的
? ? select * from world.city? where population > 10000 and population < 20000;
從 world庫的city表中? 查詢 關于 10000<人口< 20000 的
查詢中國 或 美國的城市信息
select *? ?from? world.city where countrycode='CHN' or countrycode='USA';
select *? from? world.city? where countrycode in ('CHN','USA');
建議吧以上兩種 改寫為下邊的這種? 因為這樣效率比較快
select *? from? world.city? where countrycode='CHN'
union [all]
select *? from? world.city? where countrycode='USA';
? group by子句:配合聚合函數應用
?常用聚合函數
平均 AVG()
個數 count()
總和 SUM()
最大 MAX()
最小 MIN()
集合 GROUP_CONCAT()
例子1:統計每個國家的總人口
select countrycode,SUM(population) from city group by countrycode;
例子2:統計每個國家的城市個數
select countrycode,count(id) from city group by countrycode;
從city 這個表中 以國家為基準 統計國家城市的個數
1. 拿什么為基準
? ? ? ? ? ? ? ? ? ? ? GROUP BY? countrycode
? ? ? ? ? ? ? ? 2. 統計什么東西
? ? ? ? ? ? ? ? ? ? ? 城市id,name
? ? ? ? ? ? ? ? 3. 統計東西的種類是什么?
? ? ? ? ? ? ? ? ? ? ? COUNT(id)
例子3:統計每個國家的省名字列表
SELECT countrycode,GROUP_CONCAT(district)?
FROM city
GROUP BY countrycode;
從city 這個表中 以國家為基準 統計國家 和 城市名稱的統計
例子4:統計中國每個省的城市名列表
select District,GROUP_CONCAT(NAME)
from city
where countrycode='CHN'
GROUP BY district;
從city? 這個表中? 以城市為基準? 統計城市和名稱 關于中國的
例子5:統計中國每個省的總人口數
select district,SUM(population)
from city
where countrycode='CHN'
GROUP BY district;
從city? 這個表中? 以城市為基準 統計城市和人口總數 關于中國的
HAVING? ? 比較條件
? ? ? ? --- 統計中國,每個省的總人口大于1000w的省及人口數
? ? ? ? SELECT? district ,SUM(population) FROM city
? ? ? ? WHERE countrycode='CHN'
? ? ? ? GROUP BY district
? ? ? ? HAVING? SUM(population)>10000000
? ? ? ? 說明: having后的條件是不走索引的,可以進行一些優化手段處理。
? ? ORDER BY? 排序 從小到大? 從大到小
? ? ? ? SELECT? district ,SUM(population)
FROM city
? ? ? ? WHERE countrycode='CHN'
? ? ? ? GROUP BY district
? ? ? ? ORDER BY SUM(population) DESC? ;
? ? ? ? --- 例子:查詢中國所有的城市,并以人口數降序輸出
? ? ? ? SELECT *
FROM city
WHERE countrycode='CHN'
ORDER BY? population DESC;人口數從大到小輸出
? ? LIMIT
? ? ? ? SELECT *
? ? ? ? FROM city
? ? ? ? WHERE countrycode='CHN'
? ? ? ? ORDER BY? population DESC
? ? ? ? LIMIT 5;? 顯示5行
? ? ? ? SELECT *
? ? ? ? FROM city
? ? ? ? WHERE countrycode='CHN'
? ? ? ? ORDER BY? population DESC
? ? ? ? LIMIT 10; 顯示10行
? ? ? ? SELECT *
? ? ? ? FROM city
? ? ? ? WHERE countrycode='CHN'
? ? ? ? ORDER BY? population DESC
? ? ? ? LIMIT 5,3;跳過頭5行 顯示隨后3行
? ? ? ? LIMIT M,N? ? 跳過M行,顯示N行