1.全套裝備
select [select選項] 字段列表[字段別名]/* from 數據源[where 條件子句] [group by條件子句] [having 子句] [order by 子句] [limit 子句];
什么是select選項呢?select選項是值select對查出來的結果的處理方式,主要有兩種。all:默認的,保留所有的結果;distinct: 對查詢結果進行去重;
Select select_expr [,select_expr…] [ From table_references [ where where_condition] [ group by {col_name | position} [asc|desc],… ] [ having where_condition ] [ order by {col_name | expr | position} [asc|desc],… ] [ limit {[offset,] row_count|row_count offset offset} ] ]
查詢順序:select→where→group by→having→order by→limit
2.表中所有信息的查詢 *
select * from student;
3.去重 distinct
select distinct class from student; select distinct class,teacher from student;
顯然,所查詢的列的值組合起來的行完全一樣才會被去重。
4.指定列排序 order by,asc,desc
select * from student order by col_name {asc|desc};
asc 數值 :遞增,字母:字典序
desc 數值:遞減, 字母:反序
5.別名 as
select 列名 as 別名[,列名2 as 別名,...] from table_name;
6.列查詢(不同順序結果不同)
select col_name[,col_name2...] from table_name;
7.聚合函數查詢
sum()、avg()、max()、min()分別表示對某一列求和、平均、最大、最小。
count()表示查詢記錄出現的數量,count(列)不算null值的,count(*)有算null值。
select sum(age),avg(age),max(id),min(age),count(teacher) from student
8.查詢過濾 where(where子句不能用別名)
添加了幾條記錄測試模糊查詢
模糊查詢的通配符:"%"表示任意長度的字符串(包括空字符但不包括空值),“_”表示單個字符。這里的漢字和字母都算1個字符。
Select id as myid, name as myname ,teacher from student where teacher like ‘老%’;--查詢以"老"開頭的 Select id as myid, name as myname ,teacher from student where teacher like ‘%’;--查詢所有記錄,除了null Select * from student where teacher like ‘老_’;--查詢以老開頭的兩個字的記錄
9.限制查詢數量 limit
select * from student where age<30 limit 4;--默認從第一條記錄開始搜尋,查找出4條記錄就停止 select * from student limit 2,5;--記錄的索引從0開始,這里限制從索引為2的開始即第3條語句,查找出5條語句 --如果數量不夠則顯示不出那么多記錄
10.分組查詢 group by
(1)最普通的用法,對於某一列相同的值只取1條語句,嘗試后推測應該是記錄中的第一條出現該列值的記錄
select * from student group by class;
這樣的記錄顯然沒什么用,和去重效果一樣,所以運用時需要加點東西。
(2)想知道該列的各個分組除了第一條記錄的其他記錄 group_concat(列名)
select group_concat(id),group_concat(name),age,class from student group by class;
顯然,沒有使用group_concat()函數的都只顯示一個值,id和name顯示了所有的值並且一一對應,但是沒有順序可言,第4條記錄的id可以看出。
(3)如果記錄太多看不清一個組到底有多少人,用count()函數嵌套
select count(*),group_concat(name),group_concat(teacher),class from student group by class; select count(*),group_concat(name),count(teacher),class from student group by class;
由圖可知這樣查詢非常人性化,注意區分count(*)和count(列名)對於null值的計數。
其他聚合函數同理,例如查詢每個班最大年齡的人則可以用max()函數嵌套。
11.二次篩選 having
擔心數據不夠又加了幾組記錄
where和having的區別:where作用於表或視圖,從中選擇滿足條件的元組。having短語作用於組,從中選擇滿足條件的組(所以一般與group by配合使用)。where是從查詢滿足條件的數據,用於查詢數據之前;having用於在查出的數據中挑選滿足條件的數據,在數據查出來之后處理。並且having的子句不能使用沒有查出來的列名,where則可以,如下:
12.子查詢 where
出現在其他sql語句內的select子句嵌套在查詢內部,必須始終出現在圓括號內;可以包含多個關鍵字或條件 distinct,group by,order by,limit和函數等;子查詢的外層查詢可以是 select insert update set do 等。子查詢返回的結果可以是標量、一行、一列或者是子查詢。
select * from t1 where col1 = ( select col2 from t2);
13.連接查詢
左表 inner|left|right join 右表 on 條件
(1)內連接
查詢左右表的交集
(2)左連接
左表全部查出來;右表顯示符合搜索條件的記錄,不符合條件的記錄顯示為NULL
(3)右連接
右表全部查出來;左表顯示符合搜索條件的記錄,不符合條件的記錄顯示為NULL
select A.id,B.name,C.age from Atable A inner|left|right join Btable B on A.id=B.id inner|left|right join Ctable C on A.id=C.id where ...
(4)例題
有兩個表:
表1dept包含列dept_id,dept_name,parent_id,bi_parent_id
表2dept_thirdparty包含列dept_id,thirdparty_id
關系:
兩個表以dept_id關聯,表1中每個dept_id對應一個dept_name、parent_id、bi_parent_id,每個parent_id和bi_parent_id也是一個dept_id,表2中每個dept_id對應一個thirdparty_id。
要求:
查詢出parent_id和bi_parent_id不同的dept_id(門店)的thirdparty_id,dept_name,parent_id對應的thirdparty_id,parent_id對應的dept_name,bi_parent_id對應的thirdparty_id,bi_parent_id對應的dept_name
SELECT b. thirdparty_id, a. dept_name, c. thirdparty_id 'parent_id對應的thirdparty_id', d. dept_name 'parent_id對應的dept_name', e. thirdparty_id 'bi_parent_id對應的thirdparty_id', f. dept_name 'bi_parent_id對應的dept_name' FROM dept a INNER JOIN dept_thirdparty b ON a. dept_id = b. dept_id INNER JOIN dept_thirdparty c ON a. parent_id = c. dept_id INNER JOIN dept d ON c. dept_id = d. dept_id INNER JOIN dept_thirdparty e ON a. bi_parent_id = e. dept_id INNER JOIN dept f ON e. dept_id = f. dept_id WHERE a. parent_id != a. bi_parent_id