Sql
查詢
查詢是整個(gè)數(shù)據(jù)庫的重點(diǎn),基本內(nèi)容占整個(gè)sql的大部分內(nèi)容
學(xué)習(xí)重點(diǎn):
1. 字段名中的聚合函數(shù)
2. 多表聯(lián)查
3. 條件語句(重點(diǎn)中的重點(diǎn))
1. 一個(gè)基本sql查詢語句
select field //select,顯示一個(gè)列的內(nèi)容,select哪個(gè)列名,就能顯示那一個(gè)列的數(shù)據(jù)
//需要查出一個(gè)表的所有列時(shí)(其實(shí)就是這個(gè)表的所有內(nèi)容),field是 "*" 號(hào)
from talbe_name //from,指定查詢的內(nèi)容來源于哪張表
2. distinct:去重復(fù)(select)
<blockquote><font color= "#6F00D2" >可以去掉表中的重復(fù)值
(比如一個(gè)人買了很多菜,但是我只是想這個(gè)人是誰)</font></blockquote>
select DISTINCT field
from table_name
where id= 5 //這里增加條件,是因?yàn)槿ブ貢?huì)保留第一個(gè)被查到數(shù)據(jù),
//有可能不是我們想要的那條
3. where條件子句(另起一行)
<font color= "#6F00D2">有條件地查詢數(shù)據(jù)</font>
select field
from table_name
where // 字段名 +基本/進(jìn)階條件語句
3.1基本條件語句("=""<>"(不等于),"<",">""<=",">=")(where)
<font color= "#6F00D2">一些基本的查詢</font>
select D field
from table_name
where id= 5 //"="可以替換的符號(hào)有"<>"(不等于),"<",">""<=",">="
where id = "小明" //字符串只能使用括號(hào)
3.2進(jìn)階條件語句(BETWEEN AND)(where)
<font color= "#6F00D2">查詢兩個(gè)值之間數(shù)據(jù),值的內(nèi)容可以是數(shù)值,字符串,和時(shí)間</font>
select column_name
from table_name
where column_name BETWEEN value1 AND value2 //是否顯示兩個(gè)值本身,不同數(shù)據(jù)庫處理是不同的
NOT BETWEEN value1 AND value2 //not是指此條件內(nèi)容之外的內(nèi)容
3.3進(jìn)階條件語句(LIKE)(where)
<font color= "#6F00D2">LIKE用于模糊查找,需要搭配正則表達(dá)式。</font>
select column_name
from table_name
where column_name LIKE "n%" //需要正則表達(dá)式使用
3.4關(guān)系運(yùn)算符(NOT/AND/OR)(where)
<font color= "#6f00D2">這三個(gè)單詞可以出現(xiàn)在wher子句中,對(duì)整個(gè)where子句進(jìn)行修飾,三者優(yōu)先級(jí)順序是NOT>AND>OR</font>
select column_name
from table_name
where column_name =1 AND column_id = 2 //當(dāng)數(shù)據(jù)滿足所有條件,數(shù)據(jù)才會(huì)被查出
where column_name =1 OR column_id = 2 //當(dāng)數(shù)據(jù)滿足若干條件中隨便一個(gè),數(shù)據(jù)就可以被查出
NOT where column_name =1 and column_id = 2
//AND和OR組合查詢
WHERE (FirstName='Thomas' OR FirstName='William')
AND LastName='Carter'
4. ORDER BY排序子句(另起一行)
<font color = "#6f00D2">用于進(jìn)行排序可以和where一起使用,放在where后</font>
select column_name
from table_name
ORDER BY Company //以字母順序顯示公司名稱,如果是按照company是數(shù)字,則按數(shù)字排列,如果有where子句,要放在where后面
//多字段排序
ORDER BY Company,name //這種做法目的是為了解決第一個(gè)字段排序可能會(huì)有大量重復(fù),然后需要對(duì)重復(fù)內(nèi)容在按照第二字段進(jìn)行排序
4.1倒序和正序(DESC ASC)(order by)
select column_name
from table_name
ORDER BY Company DESC/ASC //升降序需要配合Orderby一起使用,放在句尾
5. TOP子句(LIMIT/"<=")(select)
<font color = "#6f00D2">規(guī)定要返回記錄的數(shù)量,在大量數(shù)據(jù)時(shí)比較有用
</font>
select TOP 5 * //這是sqlserver寫法,各數(shù)據(jù)庫所用關(guān)鍵字不同
from table_name
select column_name //這是mysql寫法,各數(shù)據(jù)庫所用關(guān)鍵字不同
from table_name
LIMIT 5
select column_name //這是oralce寫法,各數(shù)據(jù)庫所用關(guān)鍵字不同
from table_name
where ROWNUM <=5 //ROWNUM是固定的關(guān)鍵字
select 50 PERCENT *
from Persons
6. IN操作符(where)
<font color="6f00d2">可以查找在同一個(gè)字段內(nèi)查找多個(gè)值,但是要求值是同一個(gè)字段</font>
select column_name(s)
from table_name
where column_name IN (value1,value2,...)
小結(jié):
<table>
<tr>
<th>操作符</th>
<th>區(qū)別</th>
<th>條件要求</th>
</tr>
<tr>
<td align="center">and</td>
<td>and是用來查找精確數(shù)據(jù)的,條件越多,查詢越精確</td>
<td>and可以連接多個(gè)條件</td>
</tr>
<tr>
<td align="center">or</td>
<td>or是用來查找精確數(shù)據(jù)的,會(huì)將滿足任意條件的數(shù)據(jù)都查找出來</td>
<td>or可以連接多個(gè)條件</td>
</tr>
<tr>
<td align="center">between and</td>
<td>是用來查找在兩個(gè)點(diǎn)之內(nèi)的所有數(shù)據(jù)</td>
<td>只能查找一個(gè)條件內(nèi)一個(gè)區(qū)域的數(shù)據(jù)</td>
</tr><tr>
<td align="center">in</td>
<td>是用來查找?guī)讉€(gè)值的數(shù)據(jù)</td>
<td>只能查找一個(gè)條件內(nèi)若干個(gè)數(shù)據(jù)</td>
</tr>
</table>
AS(別名語句)(select/from)
<font color="#6f00D2">在顯示的時(shí)候使用別名,方便非開發(fā)人員查看,但是不會(huì)導(dǎo)致數(shù)據(jù)庫字段被修改,as可以既可以改字段名,又可以改表名,也可以同時(shí)改名</font>
select column_name(s)
from table_name AS alias_name //現(xiàn)在是給表起別名
select column_name as 姓名 //現(xiàn)在是給字段起別名
from talbe_name
JOIN(多表聯(lián)查)(from)
<font color= "#6f00D2">作用是將若干個(gè)表生成一個(gè)大的臨時(shí)表,join根據(jù)不同情況有多種
<center><img src= "http://www.runoob.com/wp-content/uploads/2019/01/sql-join.png" width= "400 " height= "300"></center>
這兩個(gè)表作為join的參考內(nèi)容,方便理解join關(guān)鍵字的意思</font>
[圖片上傳失敗...(image-65797f-1556258177738)]
<center>原表</center>
INNER JOIN(JOIN)(from)
<font color= "#6f00D2">取得兩個(gè)表的交集部分,也被稱為內(nèi)連接</font>
<center><img src= "http://www.runoob.com/wp-content/uploads/2013/09/img_innerjoin.gif"></img></center>
select*
from student Join score
輸出結(jié)果
[圖片上傳失敗...(image-8f55ac-1556258177738)]
明顯的是,輸出的結(jié)果不是我們想要的,顯示的是25條數(shù)據(jù),就是把兩個(gè)表進(jìn)行了5*5配對(duì)而已,所以我們需要引入on
ON(另起一行)
<font color= "#6f00D2">on的作用類似于where,但是on只能和join一起使用,只存在join中的條件語句</font>
SELECT *
FROM STUDENT JOIN SCORE
ON di1 = di2
結(jié)果如下
<center><img src="https://f3p3zw.bl.files.1drv.com/y4mKCmpqTEL_-3Z__8bd5XL-9teN-H-39yonn-k37sEsKshns5jhWa6FUdMevOTpCCSXxuRCcgMbY4ubBhzihp-5BxrZ5eyyAVLwkfv-by1-I44gX5TFzKBbOu0luo6H4W26NSX4_zIg1SzXMMMz6E6-mvGC6-PIrYW0PV8y7m7-1VVnD93-jW9xlPsVaM4kWBIdaYrLTL5zxaY2lLv4msefA?width=778&height=418&cropmode=none"></img></center>
<center>on作為限制條件,只顯示id1和id2相等的數(shù)據(jù)</center>
LEFT JOIN(JOIN)(from)
<center><img src= "http://www.runoob.com/wp-content/uploads/2013/09/img_leftjoin.gif"></img></center>