MYSQL必知必會(huì)筆記

SELECT語(yǔ)句

使用select語(yǔ)句,必須至少有兩條信息,想選擇什么,從哪選擇。

單個(gè)語(yǔ)句結(jié)束必須加上分號(hào),SQL語(yǔ)句不區(qū)分大小寫(xiě)。

檢索單個(gè)列

select pro_name from products

檢索多個(gè)列

在多個(gè)列名并列,使用逗號(hào)分隔開(kāi)來(lái)

select pro_id,pro_name,pro_price from products

檢索所有列

使用通配符'*'來(lái)標(biāo)識(shí)所有的列

select * from products

檢索不同的行

使用select檢索某一列時(shí),會(huì)將該列中所有行的數(shù)據(jù)都檢索出來(lái),如下圖所示

Distinct.png

如何檢索行值不同的列,使用DISTINCT關(guān)鍵字,只返回行值不同的列

select distinct vend_id from products

DISTINCT關(guān)鍵字默認(rèn)作用在所有的列上,而不僅僅是前置列。

限制結(jié)果

LIMIT關(guān)鍵字可以限制返回的行數(shù)

select prod_name from products limit 5,5;

選取從第五行開(kāi)始的五行。MySQL行計(jì)數(shù)從0開(kāi)始 ,從行0開(kāi)始選擇,則省略0

排序檢索數(shù)據(jù)

排序數(shù)據(jù)

如果希望選擇出來(lái)的數(shù)據(jù)根據(jù)某個(gè)列的值進(jìn)行排序,可以使用ORDER BY關(guān)鍵字

select prod_name from products order by prod_name;

按多個(gè)列排序數(shù)據(jù)

與select選擇多列一樣,只需用逗號(hào)將列名分隔開(kāi)來(lái)即可

select prod_name from products order by prod_price,prod_name

指定排序方向

order by關(guān)鍵字的默認(rèn)排序方向時(shí)增序,如果想要降序使用DESC關(guān)鍵字

select pro_name from products order by prod_prices desc

DESC關(guān)鍵字默認(rèn)只作用于關(guān)鍵字前面的那個(gè)列,其與DISTINCT關(guān)鍵字不同,如果想要多個(gè)列都適用降序,則在每個(gè)列之后都增加DESC關(guān)鍵字。

ORDER BY關(guān)鍵字與LIMIT搭配使用,可以選擇指定的幾行,比如說(shuō)選擇價(jià)格最低的商品的名字

select prod_name from products order by prod_price desc limit 1;

LIMIT語(yǔ)句應(yīng)該位于ORDER BY語(yǔ)句之后

過(guò)濾語(yǔ)句

WHERE過(guò)濾語(yǔ)句

使用select語(yǔ)句時(shí)可以用where設(shè)置搜索條件

where.png

用BETWEEN關(guān)鍵字的時(shí)候,需要范圍的開(kāi)始值和結(jié)束值,比如

select prod_name from products where prod_price between 5 and 10

在表中,當(dāng)一個(gè)列不包含值,稱其為空值NULL,與字段包含0、空字符串或僅僅包含空格不同

WHERE語(yǔ)句也可以用來(lái)直接判空

select prod_name from products where prod_price is NULL

ORDER BY語(yǔ)句可以與WHERE語(yǔ)句搭配,置于WHERE語(yǔ)句之后

AND和OR操作符可以用來(lái)增加WHERE語(yǔ)句的多樣性

select prod_name from products where prod_id=1 or prod_id=12
select pro_name from products where vend_id=1003 and prod_price<=10;

在計(jì)算的時(shí)候,AND的優(yōu)先級(jí)高于OR的優(yōu)先級(jí),根據(jù)實(shí)際情況,對(duì)AND或者OR并列的子句進(jìn)行加括號(hào)處理

IN關(guān)鍵字

WHERE語(yǔ)句用來(lái)指定要匹配值的清單關(guān)鍵字,功能與OR相當(dāng)

對(duì)于IN的合法取值由逗號(hào)分隔開(kāi)全部在圓括號(hào)中

select prod_name from products where vend_id in (1002,1003) order by prod_name

為什么要使用IN操作符:

  • IN操作符的比OR的執(zhí)行速度更快
  • IN的最大優(yōu)點(diǎn)是可以包含其他的SELECT語(yǔ)句

NOT關(guān)鍵字

WHERE語(yǔ)句中的NOT操作符有且只有一個(gè)功能就是否定它之后所跟的任何條件

select prod_name from products where vend_id not in (1002,1003) order by prod_name

LIKE通配符

使用LIKE關(guān)鍵字以及‘%’符號(hào)可以對(duì)字符串內(nèi)容進(jìn)行模糊匹配,‘%’可以匹配多個(gè)甚至0個(gè)字符,但是不能匹配NULL內(nèi)容;‘_’只能匹配單個(gè)字符

select prod_id from products where prod_name like '%anvil%'
select prod_id from products where prod_name like '_nvil'

正則表達(dá)式匹配

正則表達(dá)式意義為只要列值中出現(xiàn)我想要匹配的字符,即可進(jìn)行返回

基本匹配

select prod_id from products where prod_name regexp '1000'

‘.’是正則表達(dá)式中的一個(gè)特殊字符,可以表示任意內(nèi)容

OR匹配

可以使用OR關(guān)鍵字進(jìn)行或的匹配

select prod_id from products where prod_name regexp '1000|2000'

[ ] 匹配多個(gè)字符

select prod_id from products where prod_name regexp '[123] ton'

這里的[123]就是上述關(guān)鍵字OR表示的1|2|3的意思,如果是一個(gè)范圍,可以用[1-5]這樣的形式來(lái)表示,不僅僅是數(shù)字,字母也可以[a-z]

特殊字符匹配

如果想要匹配‘.’這樣的特殊字符如何匹配呢,上面也提到‘.’可以匹配任何字符

使用’\\‘進(jìn)行轉(zhuǎn)義

select prod_id from products where prod_name regexp '\\.'

拼接字段和別名

選擇列時(shí)可能會(huì)希望將幾個(gè)列的值并列在一起顯示,并且指定為一個(gè)新的名字。

拼接的關(guān)鍵字是Concat函數(shù),別名的關(guān)鍵字是AS函數(shù)

select concat(vend_name,' (',vend_country,' )') as vend_title from products order by vend_name
concat.png

計(jì)算字段

MySQL還可以對(duì)檢索出來(lái)的數(shù)據(jù)進(jìn)行數(shù)學(xué)計(jì)算,包括+、*、-、/

select prod_id,quantity,item_price,quantity*item_price as expanded_price from products where order_num=20005
計(jì)算字段.png

聚集函數(shù)

聚集函數(shù).png
select avg(prod_price) as avg_price from products;
select count(*) as row_sum from products;//count(*)對(duì)表中所有的行進(jìn)行計(jì)數(shù),不管表中包含的是空值(NULL)還是非空值
select count(prod_id) as id_num from products;//只對(duì)列中具有值的行進(jìn)行計(jì)數(shù),忽略NULL值
select max(prod_price) as max_price from products;
select min(prod_price) as min_price from products;
select sum(prod_price) as sum_price from products;

以上五個(gè)聚集函數(shù)默認(rèn)都是對(duì)所有的行進(jìn)行計(jì)算,如果使用DISTINCT關(guān)鍵字在列名之前,默認(rèn)只會(huì)計(jì)算不同的值

select avg(distinct prod_price) as avg_price from products;//只計(jì)算價(jià)格不同值的平均值
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容