MySQL語句的執(zhí)行順序如下:
| 執(zhí)行序號 | 執(zhí)行操作 | 解釋 |
|---|---|---|
| 1 | from | 對from的左邊的表的輸出,會作為右邊的表的輸入計算笛卡爾積得到虛擬表T1 |
| 2 | on | 對虛表T1進(jìn)行on過濾,過濾出所有滿足on謂詞條件的列,生成虛擬表T2 |
| 3 | join | 如果指定了outer join(比如left join、 right join),那么保留表中未匹配的行就會作為外部行添加到虛擬表T2中,產(chǎn)生虛擬表T3, from子句中包含兩個以上的表的話,那么就會對上一個join連接產(chǎn)生的結(jié)果VT3和下一個表重復(fù)執(zhí)行步驟1~3這三個步驟,一直到處理完所有的表為止。 |
| 4 | where | 對虛擬表T3進(jìn)行where條件過濾。只有符合<where-condition>的記錄才會被插入到虛擬表T4中 |
| 5 | group by | 根據(jù)group by子句中的列,對T4中的記錄進(jìn)行分組操作,產(chǎn)生T5 |
| 6 | with (cube、rollup) | 對表T5進(jìn)行cube或者rollup操作,產(chǎn)生表T6 |
| 7 | having | 對虛擬表T6應(yīng)用having過濾,只有符合<having-condition>的記錄才會被 插入到虛擬表T7中 |
| 8 | select | 執(zhí)行select操作,選擇指定的列,插入到虛擬表T8中 |
| 9 | distinct | 對T8中的記錄進(jìn)行去重。產(chǎn)生虛擬表T9 |
| 10 | order by | 將虛擬表T9中的記錄按照<order_by_list>進(jìn)行排序操作,產(chǎn)生虛擬表T10 |
| 11 | limit | 取出指定行的記錄,產(chǎn)生虛擬表T11, 并將結(jié)果返回 |
查詢子句
命令格式: select [all|distinct] select_expr from -> where -> group by [合計函數(shù)] -> having -> order by -> limit
select_expr
ex:
-- 可以用 * 表示所有字段。
select * from student;
-- 可以使用表達(dá)式(計算公式、函數(shù)調(diào)用、字段也是個表達(dá)式)
select name, age,512*2, now() from student;
-- 可以為每個列使用別名。適用于簡化列標(biāo)識,避免多個列標(biāo)識符重復(fù)。使用 as 關(guān)鍵字,也可省略 as.
select age as student_age from student;
from 子句
用于標(biāo)識查詢來源。
ex:
-- 可以為表起別名。使用as關(guān)鍵字,也可以省略as。
select * from student as st, clazz as c;
select * from student st,clazz c;
-- from子句后,可以同時出現(xiàn)多個表。多個表會橫向疊加到一起,而數(shù)據(jù)會形成一個笛卡爾積。
select * from student, clazz;
where 子句
從from獲得的數(shù)據(jù)源中進(jìn)行篩選。
ex:
--查詢id小于10的記錄
select * from student where id < 10;
- 整型1表示真,0表示假。
- 表達(dá)式由運(yùn)算符和運(yùn)算數(shù)組成。
- 運(yùn)算數(shù):變量(字段)、值、函數(shù)返回值
- 運(yùn)算符:=, <=>, <>, !=, <=, <, >=, >, !, &&, ||, in (not) null, (not) like, (not) in, (not) between and, is (not), and, or, not, xor
is/is not (ture|false|unknown)
group by 子句, 分組子句
命令格式:group by 字段/別名 [排序方式]
分組后會進(jìn)行排序。升序:asc,降序:desc
以下[合計函數(shù)]需配合 group by 使用:
count 返回不同的非NULL值數(shù)目 count(*)、count(字段)
sum 求和
max 求最大值
min 求最小值
avg 求平均值
group_concat 返回帶有來自一個組的連接的非NULL值的字符串結(jié)果。組內(nèi)字符串連接。
ex:
--統(tǒng)計所有用戶的訂單編號,各編號間用逗號分隔
select user_id, group_concat(order_id,',') from order group by user_id;
having 子句,條件子句
與 where 功能、用法相同,執(zhí)行時機(jī)不同。
where 在開始時執(zhí)行檢測數(shù)據(jù),對原數(shù)據(jù)進(jìn)行過濾。
having 對篩選出的結(jié)果再次進(jìn)行過濾。
having 字段必須是查詢出來的,where 字段必須是數(shù)據(jù)表存在的。
where 不可以使用字段的別名,having 可以。因為執(zhí)行where代碼時,可能尚未確定列值。
where 不可以使用合計函數(shù)。一般需用合計函數(shù)才會用 having
SQL標(biāo)準(zhǔn)要求having必須引用group by子句中的列或用于合計函數(shù)中的列。
order by 子句,排序子句
order by 排序字段/別名 排序方式 [,排序字段/別名 排序方式]...
升序:asc,降序:desc
支持多個字段的排序。
limit 子句,限制結(jié)果數(shù)量子句
僅對處理好的結(jié)果進(jìn)行數(shù)量限制。將處理好的結(jié)果的看作是一個集合,按照記錄出現(xiàn)的順序,索引從0開始。
limit 起始位置, 獲取條數(shù)。
select * from student limit 10 ,10;
省略第一個參數(shù),表示從索引0開始。
limit 獲取條數(shù)
select * from student limit 10;
ps:limit和offset連用可用于分頁
distinct, all 選項
distinct 去除重復(fù)記錄
默認(rèn)為 all, 全部記錄
union
將多個select查詢的結(jié)果組合成一個結(jié)果集合。
命令格式:select ... union[all | distinct] select ....
默認(rèn) distinct 方式,即所有返回的行都是唯一的,建議對每個select查詢加上小括號包裹。order by 排序時,需加上 limit 進(jìn)行結(jié)合。需要各select查詢的字段數(shù)量一樣。每個select查詢的字段列表(數(shù)量、類型)應(yīng)一致,因為結(jié)果中的字段名以第一條select語句為準(zhǔn)。
ex:
select * from student union select * from student_temp;
from型
from后要求是一個表,必須給子查詢結(jié)果取個別名。簡化每個查詢內(nèi)的條件。from型需將結(jié)果生成一個臨時表格,可用以原表的鎖定的釋放。子查詢返回一個表,子查詢需用括號包裹。表型子查詢。
ex:
select from (select from student where id>0) as stu where id<10;
where型
子查詢返回一個值,標(biāo)量子查詢。不需要給子查詢?nèi)e名。
where子查詢內(nèi)的表,不能直接用以更新。
ex:
--取出訂單表中消費(fèi)金額最大的訂單記錄
select * from order where money = (select max(money) from order);
列子查詢
如果子查詢結(jié)果返回的是一列。使用 in 或 not in 完成查詢。
exists 和 not exists 條件 如果子查詢返回數(shù)據(jù),則返回1或0。常用于判斷條件。
ex:
select column1 from t1 where exists (select * from t2);
行子查詢,查詢條件是一個行。
ex:
select * from t1 where (id, gender) in (select id, gender from t2);
行構(gòu)造符:(col1, col2, ...) 或 row(col1, col2, ...)行構(gòu)造符通常用于與對能返回兩個或兩個以上列的子查詢進(jìn)行比較。
特殊運(yùn)算符
!= all() 相當(dāng)于 not in
= some() 相當(dāng)于 in。any 是 some 的別名
!= some() 不等同于 not in,不等于其中某一個。
all, some 可以配合其他運(yùn)算符一起使用。
連接查詢(join)
將多個表的字段進(jìn)行連接,可以指定連接條件。
連接方式|解釋|例子
---|---|
內(nèi)連接(inner join)|默認(rèn)就是內(nèi)連接,可省略inner。只有數(shù)據(jù)存在時才能發(fā)送連接。即連接結(jié)果不能出現(xiàn)空行。 on 表示連接條件。其條件表達(dá)式與where類似。也可以省略條件(表示條件永遠(yuǎn)為真) 也可用where表示連接條件。還有 using, 但需字段名相同。 using(字段名)|select * from student s join student_course sc on s.id=sc.student_id;
交叉連接 cross join|沒有條件的內(nèi)連接。|select * from tb1 cross join tb2;
外連接(outer join)|如果數(shù)據(jù)不存在,也會出現(xiàn)在連接結(jié)果中。|select * from student outer join student_course on student.id = student_course.student_id;
左外連接 left join|如果數(shù)據(jù)不存在,左表記錄會出現(xiàn),而右表為null填充|select * from student left join student_course on student.id = student_course.student_id;
右外連接 right join|如果數(shù)據(jù)不存在,右表記錄會出現(xiàn),而左表為null填充|select * from student right join student_course on student.id = student_course.student_id;
自然連接(natural join)|自動判斷連接條件完成連接。相當(dāng)于省略了using,會自動查找相同字段名。|select info.id, info.name, info.stu_num, extra_info.hobby, extra_info.sex from info, extra_info where info.stu_num = extra_info.stu_id;