MySQL基礎(chǔ)(一)——查詢語(yǔ)句
1.導(dǎo)入示例數(shù)據(jù)庫(kù)
2.SQL是說(shuō)明?MySQL是說(shuō)明?
3.查詢語(yǔ)句 SELECT FROM
語(yǔ)句解釋:
去重語(yǔ)句;
前N個(gè)語(yǔ)句:
CASE...END語(yǔ)句:
4.篩選語(yǔ)句 WHERE
語(yǔ)句解釋: 運(yùn)算符/通配符/操作符:
5.分組語(yǔ)句 GROUP BY
聚集函數(shù): 語(yǔ)句解釋: HAVING子句:
6.排序語(yǔ)句 ORDER BY
語(yǔ)句解釋: 正序、逆序:
7.函數(shù)
時(shí)間函數(shù): 數(shù)值函數(shù): 字符串函數(shù):
8.SQL注釋
9.SQL代碼規(guī)范
[SQL編程格式的優(yōu)化建議] [https://zhuanlan.zhihu.com/p/27466166](https://zhuanlan.zhihu.com/p/27466166) [SQL Style Guide] [https://www.sqlstyle.guide/](https://www.sqlstyle.guide/)
由于時(shí)間問(wèn)題,參考了知乎專欄
1. 導(dǎo)入示例數(shù)據(jù)庫(kù)
教程 MySQL導(dǎo)入示例數(shù)據(jù)庫(kù) - MySQL教程?,在上面鏈接中有示例數(shù)據(jù)庫(kù)的下載鏈接,
Navicat自帶導(dǎo)入功能,如下截圖所示:

右鍵local,點(diǎn)擊運(yùn)行SQL文件,按步驟操作,成功會(huì)顯示如下界面:

2. 查詢語(yǔ)句 SELECT FROM
SELECT是最常用的SQL語(yǔ)句,沒(méi)有之一,它的用途是從一個(gè)或多個(gè)表中檢索信息,使用SELECT檢索信息,至少要給出兩條信息:
1、想選擇什么信息;
2、從哪里選擇信息;
實(shí)例
查詢一列的數(shù)據(jù)
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="mysql" contenteditable="true" cid="n39" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SELECT contactLastName FROM customers --contactLastName是列標(biāo)簽,customers是表名</pre>
運(yùn)行結(jié)果如下所示,有122條,如下所示:

利用distinct語(yǔ)句去重
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="mysql" contenteditable="true" cid="n43" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SELECT DISTINCT contactLastName FROM customers -- contactLastName是列標(biāo)簽,customers是表名</pre>
該查詢語(yǔ)句在contactLastName加上distinct,只會(huì)保留唯一值,重復(fù)的會(huì)去掉,可以看到結(jié)果從122條變成108條,如下所示:

利用limit取前N個(gè)語(yǔ)句
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="text" contenteditable="true" cid="n47" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SELECT contactLastName FROM customers LIMIT 10 -- contactLastName是列標(biāo)簽,customers是表名</pre>
limit后面的數(shù)字可以為任意正整數(shù),只要小于表的行數(shù),結(jié)果如下:

利用CASE...END判斷語(yǔ)句
詳細(xì)參考
mysql操作查詢結(jié)果case when then else end用法舉例www.cnblogs.com
4. 篩選語(yǔ)句 WHERE
WHERE 子句配合 SELECT 語(yǔ)句,表示有條件的篩選信息,類似程序語(yǔ)言中的if條件。
where語(yǔ)句常結(jié)合運(yùn)算符、通配符操作,運(yùn)算符常見(jiàn)有算術(shù)運(yùn)算符、邏輯運(yùn)算符、比較運(yùn)算符等。
詳細(xì)參考:
MySQL(六)之MySQL常用操作符 - 苦水潤(rùn)喉 - 博客園www.cnblogs.com!圖標(biāo)
通配符

算術(shù)運(yùn)算符
“+”、“-”、“*”、“/”、“%”分別對(duì)應(yīng)加減乘除求余。
比較運(yùn)算符
“==”等于、“<=>”安全的等于、“<>(!=)”不等于、“<=”小于等于、“>=”大于等于、“IS NULL”判斷一個(gè)值是否為NULL、“IS NOT NULL”判斷一個(gè)值是否不為NULL。
邏輯運(yùn)算符
NOT或者! 邏輯非
AND或者&& 邏輯與
OR或者|| 邏輯或
XOR 邏輯異或
實(shí)例
利用where語(yǔ)句篩選
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="mysql" contenteditable="true" cid="n72" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SELECT contactLastName FROM customers WHERE country = "USA" -- contactLastName是列標(biāo)簽,customers是表名</pre>
選取國(guó)籍是USA的contactLastName,有36條信息:

where語(yǔ)句加上AND,多個(gè)條件同時(shí)滿足
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="mysql" contenteditable="true" cid="n76" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SELECT contactLastName FROM customers WHERE country = "USA" AND salesRepEmployeeNumber > 1200-- contactLastName是列標(biāo)簽,customers是表名</pre>
選取國(guó)籍是USA、salesRepEmployeeNumber > 12的contactLastName,有18條信息:

where語(yǔ)句加上OR、IS NULL
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="mysql" contenteditable="true" cid="n80" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SELECT contactLastName FROM customers WHERE country = "France" OR state IS NULL-- contactLastName是列標(biāo)簽,customers是表名</pre>
只要滿足一個(gè)條件就行,73條數(shù)據(jù),如下:

5. 分組語(yǔ)句 GROUP BY
GROUP BY 語(yǔ)句根據(jù)一個(gè)或多個(gè)列對(duì)結(jié)果集進(jìn)行分組,可以理解成Excel上的透視表。HAVING子句常常配合GROUP BY使用,和where功能相似,where對(duì)行進(jìn)行篩選,而HAVING則是對(duì)分組進(jìn)行篩序。
MySQL中提供的聚合函數(shù)可以用來(lái)統(tǒng)計(jì)、求和、求最值等等。
分類:
COUNT:統(tǒng)計(jì)行數(shù)量 SUM:獲取單個(gè)列的合計(jì)值 AVG:計(jì)算某個(gè)列的平均值 MAX:計(jì)算列的最大值 MIN:計(jì)算列的最小值
實(shí)例
按國(guó)家分類,并統(tǒng)計(jì)出現(xiàn)的次數(shù)
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="mysql" contenteditable="true" cid="n90" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SELECT country,count(*) as num FROM customers GROUP BY country</pre>
結(jié)果如下,有27個(gè)國(guó)家:

利用having語(yǔ)句,統(tǒng)計(jì)出現(xiàn)次數(shù)大于3的國(guó)家
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="mysql" contenteditable="true" cid="n94" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SELECT country,count() as num FROM customers GROUP BY country HAVING count() > 3</pre>
篩選之后,只有8條信息,結(jié)果如下:

6. 排序語(yǔ)句 ORDER BY
ORDER BY子句來(lái)設(shè)定你想按哪個(gè)字段哪種方式來(lái)進(jìn)行排序,再返回搜索結(jié)果,常常配合ASC升序、DSEC降序使用。
實(shí)例
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="mysql" contenteditable="true" cid="n100" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SELECT country,count() as num
FROM customers
WHERE creditLimit > 0
GROUP BY country HAVING count() > 3
ORDER BY country DESC ,count(*) ASC</pre>
GROUP BY語(yǔ)句常常在WHERE之后,ORDER BY之前,country是降序,國(guó)家計(jì)數(shù)是升序,結(jié)果如下:

\7. 函數(shù)
時(shí)間函數(shù)太多,只選部分函數(shù)
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="mysql" contenteditable="true" cid="n105" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SELECT NOW();-- 獲得當(dāng)前日期
SELECT TIME('2017-05-15 10:37:14.123456');-- 獲取時(shí)間
SELECT YEAR('2017-05-15 10:37:14.123456');-- 獲取年份
SELECT MONTH('2017-05-15 10:37:14.123456');-- 獲取月份
SELECT DAY('2017-05-15 10:37:14.123456');-- 獲取日
SELECT HOUR('2017-05-15 10:37:14.123456');-- 獲取時(shí)
SELECT FROM_UNIXTIME(1494815834);-- 將時(shí)間戳轉(zhuǎn)為具體時(shí)間</pre>
數(shù)值函數(shù)

字符串函數(shù)

這一節(jié)實(shí)例相對(duì)簡(jiǎn)單,不做具體說(shuō)明。
\8. SQL注釋
“--”之后的文本屬于注釋。
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="mysql" contenteditable="true" cid="n113" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SELECT NOW();-- 獲得當(dāng)前日期</pre>
“//”從/開(kāi)始,到/結(jié)束,/和/之間的任何內(nèi)容都是注釋
\9. SQL代碼規(guī)范
SQL編程格式的優(yōu)化建議(https://zhuanlan.zhihu.com/p/27466166)
SQL Style Guide(http://link.zhihu.com/?target=https%3A//www.sqlstyle.guide/)
題目一:
數(shù)據(jù)表如下如圖所示,查找重復(fù)的電子郵箱

利用HAVING的篩選分組功能
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="mysql" contenteditable="true" cid="n122" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SELECT Email as NUM FROM email GROUP BY Email HAVING count(Email) > 1</pre>
輸出:

題目二:
如果一個(gè)國(guó)家的面積超過(guò)300萬(wàn)平方公里,或者(人口超過(guò)2500萬(wàn)并且gdp超過(guò)2000萬(wàn)),那么這個(gè)國(guó)家就是大國(guó)家。
編寫一個(gè)SQL查詢,輸出表中所有大國(guó)家的名稱、人口和面積。

利用where和運(yùn)算符做篩選
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="mysql" contenteditable="true" cid="n130" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">SELECT name,population, area FROM world WHERE area > 3000000 OR (population > 25000000 AND gdp > 20000000)</pre>
結(jié)果如下,和預(yù)期一致: