MySQL常用查詢
一、單表查詢
1. 查詢所有 *
mysql> select * from student;
2. 查詢選中欄位記錄
mysql> select s_name from student;
3. 條件查詢 where
mysql> select s_name from student where s_id<5;
4.查詢後為欄位重新命名 as
mysql> select s_name as 名字 from student;
5.模糊查詢 like
%匹配多個字元
mysql> select s_name as 姓名 fromstudent where s_name like '李%';
_匹配一個字元
mysql> select s_name as 姓名 from student where s_name like '李_';
6. 排序(預設升序) order by 以某個欄位為主進行排序
升序 asc (asc可以不寫)
mysql> select * from student order by sc_id asc;
降序 desc
mysql> select * from student order by sc_id desc;
7. 限制顯示資料數量 limit
limit 只接一個數字n時表示顯示前面n行
mysql> select * from student limit 5;
limit 接兩個數字m,n時表示顯示第m行(不包括第m行)之後的n行
mysql> select * from student limit 2,4;
8. 常用聚合函式
mysql> select * from details;
最大值 max
mysql> select max(age) from details;
最小值 min
mysql> select min(age) from details;
求和 sum
mysql> select sum(age) from details;
平均值 avg
mysql> select avg(age) from details;
四捨五入 round
mysql> select round(avg(age)) from details;
統計 count
mysql> select count(address) from details;
注1:
1、count(1):可以統計表中所有資料,不統計所有的列,用1代表程式碼行,在統計結果中包含列欄位為null的資料;
2、count(欄位):只包含列名的列,統計表中出現該欄位的次數,並且不統計欄位為null的情況;
3、count(*):統計所有的列,相當於行數,統計結果中會包含欄位值為null的列;
注2:count執行效率
列名為主鍵,count(列名)比count(1)快;列名不為主鍵,count(1)會比count(列名)快;
如果表中多個列並且沒有主鍵,則count(1)的執行效率優於count(*);
如果有主鍵,則select count(主鍵)的執行效率是最優的;如果表中只有一個欄位,則select count(*)最優。
例:
CREATE TABLE `user` ( `id` int(5) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(10) DEFAULT NULL, `password` varchar(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
測試資料為:
1 name1 123456
2 name2 123456
3 name3 123456
4 name4 NULL
進行查詢:
mysql>select count(*) from `user` mysql>select count(name) from `user` mysql>select count(password) from `user`
得到的結果:4,4,3
原因:
1,count(*)是對行數目進行計數,所以結果為4。
2,count(column_name)是對列中不為空的行進行計數,所以count(name)=4,而count(password)=3。
9. 分組查詢 group by
篩選條件使用having,having後接條件必須是select後存在的欄位
mysql> select age,count(age) from details group by age having age>30;
以age為組統計每個age的人數最後篩選出age大於30的
二、子查詢(巢狀查詢)
mysql> select * from details where age>(select avg(age) from details);
查詢所有age大於平均年齡的資訊
三、關聯查詢
內連線 inner join
無條件內連線 又稱笛卡爾連線
mysql> select * from student inner join college;
有條件內連線 在無條件基礎上,on接條件
mysql> select * from student inner join college on sc_id=c_id;
外連線
- 左外連線 left join
以左表為基準,右表沒有對應資料以null填充,多餘資料去除
mysql> select * from tb1 left join tb2 on id=t_id;
mysql> select * from tb2 left join tb1 on id=t_id;
- 右外連線 right join
以右表為基準,左表沒有對應資料以null填充,多餘資料去除
mysql> select * from tb1 right join tb2 on id=t_id;
mysql> select * from tb2 right join tb1 on id=t_id;
派生表必須命名 as
mysql> select * from (select * from details where age>30) as a left join student on d_id=s_id;
四、去重 distinct 的用法
在使用MySQL時,有時需要查詢出某個欄位不重複的記錄,這時可以使用mysql提供的distinct這個關鍵字來過濾重複的記錄,但是實際中我們往往用distinct來返回不重複欄位的條數(count(distinct id)),其原因是distinct只能返回他的目標欄位,而無法返回其他欄位,
在使用distinct的過程中主要注意一下幾點:
- 在對欄位進行去重的時候,要保證distinct在所有欄位的最前面
- 如果distinct關鍵字後面有多個欄位時,則會對多個欄位進行組合去重,只有多個欄位組合起來的值是相等的才會被去重
下面我們通過在開發過程中經常遇到的一些關於distinct的例項來加深大家對該關鍵字用法的理解:
資料庫表結構和資料如下圖所示:
- 對單個欄位進行去重sql
select distinct age from user; 查詢結果 age 10 20 30
- 對多個欄位進行去重sql:
select distinct name,age from user; 查詢結果 name age One 10 Zero 20 Two 20 Four 30 One 30
- 對多個欄位進行去重並求count的sql(實際中我們往往用distinct來返回不重複欄位的條數(count(distinct id)),其原因是distinct只能返回他的目標欄位,而無法返回其他欄位):
select count(distinct name,age) as total from user; 查詢結果 total 5
- 對select * 進行去重
select distinct * from user; 由於 * 代表所有欄位,所以該sql和 select distinct id,name,age,sign from user 語義相同 查詢結果: id name age sign 1 One 10 夢想要有的,萬一實現了呢 2 Zero 20 http://www.chaoshizhushou.com 3 Two 20 OneZeroTwoFour 4 Four 30 加油 5 One 30 學習才是硬道理 6 Four 30 一日三省吾身
如果sql這樣寫:select id,distinct name from user,這樣mysql會報錯,因為distinct必須放在要查詢欄位的開頭。
所以一般distinct用來查詢不重複記錄的條數。
如果要查詢不重複的記錄,有時候可以用group by :
select id,name from user group by name;
五、group by 的用法
1. group by的常規用法
group by的常規用法是配合聚合函式,利用分組資訊進行統計,常見的是配合max等聚合函式篩選資料後分析,以及配合having進行篩選後過濾。
- 聚合函式max
- selectmax(user_id),gradefrom user_infogroupby grade ;
這條sql的含義很明確,將資料按照grade欄位分組,查詢每組最大的user_id以及當前組內容。注意,這裡分組條件是grade,查詢的非聚合條件也是grade。這裡不產生衝突。
having
select max(user_id),grade from user_info group by grade having grade>'A'
這條sql與上面例子中的基本相同,不過後面跟了having過濾條件。將grade不滿足’>A’的過濾掉了。注意,這裡分組條件是grade,查詢的非聚合條件也是grade。這裡不產生衝突。
2. group by的非常規用法
select max(user_id),id,grade from user_info group by grade
這條sql的結果就值得討論了,與上述例子不同的是,查詢條件多了id一列。資料按照grade分組後,grade一列是相同的,max(user_id)按照資料進行計算也是唯一的,id一列是如何取值的?看上述的資料結果,
推論:id是實體記憶體的第一個匹配項。
究竟是與不是需要繼續探討。
3. 結論
- 當group by 與聚合函式配合使用時,功能為分組後計算
- 當group by 與having配合使用時,功能為分組後過濾
- 當group by 與聚合函式,同時非聚合欄位同時使用時,非聚合欄位的取值是第一個匹配到的欄位內容,即id小的條目對應的欄位內容。