[學(xué)習(xí)筆記]數(shù)據(jù)庫SQL語句

1.檢索不同的行

查詢數(shù)據(jù),過濾指定列名重復(fù)的數(shù)據(jù),在列名前面添加 distinct 關(guān)鍵字

例:select distinct vend_id from products
2.限制結(jié)果

查詢數(shù)據(jù),指定查詢開始行和查詢數(shù)量,使用 limit 關(guān)鍵字

-例:select vend_id from products limit 5    //第五行開始
-例:select vend_id from products limit 5,4    //第五行開始,查詢4行
3.排序查詢

查詢數(shù)據(jù),指定列名進(jìn)行排序,使用 order by 關(guān)鍵字

-例:select * from products order by id    //以id進(jìn)行排序
-例:select * from products order by id,price    //在id排序后的基礎(chǔ)上以price排序
*可使用 desc 設(shè)置降序查詢,asc 設(shè)置升序查詢[默認(rèn)排序方式
4.WHERE檢索
where子操作符
-例:select * from products where id <> 1   
-例:select * from products where id between 5 and 10   
-例:select * from products where price is null    //查詢無值數(shù)據(jù)
5.WHERE其他子句

AND操作符、OR操作符、IN操作符、NOT操作符

-例:select * from products where id in(5,6)    //檢索id等于5、id等于6的數(shù)據(jù)
-例:select * from products where id not in(5,6)    //取反
6.LIKE操作符

百分號(hào)(%)通配符,'jet%'是以jet開頭的數(shù)據(jù)、下劃線通配符,'_ ton anvil'同理,單字符檢索

-例:select * from products where id like 'jet%'  
-例:select * from products where id like '_ ton anvil'    //.5 ton anvil不符合要求
7.正則表達(dá)式

使用 regexp 關(guān)鍵字,按照要求進(jìn)行查詢數(shù)據(jù),與like不同,只要文本中包含查找的數(shù)據(jù)就匹配。

-例:select * from products where id regexp '.00'      //.表示匹配任意一個(gè)字符
-例:select * from products where id regexp '100|200'      //  |表示or
-例:select * from products where id regexp '[123] TON'    //  [123]等同于 ‘1|2|3’,[^123]代表除這些字符意外的
-例:select * from products where id regexp '[0-9] TON'    //  [0-9]等同于[0123456789],也可以寫字母[a-z]
-例:select * from products where id regexp '[0-9] TON'    //  [0-9]等同于[0123456789],也可以寫字母[a-z]
匹配特殊字符
-例:select * from products where id regexp '\\.'      //  \\表示查詢特殊字符,\\.查詢.。
-例:select * from products where id regexp '\\\'      //  \\\查詢\。
匹配字符類

匹配元字符
-例:select * from products where id regexp '\\([0-9] sticks?\\)'      //查詢小括號(hào),0-9的數(shù)字,可有可無的s,如stick或sticks
-例:select * from products where id regexp '[[:digit:]]{4}'      //查詢4位數(shù)字
定位元字符
-例:select * from products where id regexp '^[0-9\\.]'      //匹配開始第一位為數(shù)字或.。
8.拼接字段

將兩個(gè)字段的信息合并為一個(gè)字段顯示,使用 Concat() 關(guān)鍵字

例:select Concat(vend_id,'(', vend_name,')') from products      //輸出 1(汪強(qiáng))
9.字段別名(導(dǎo)出列)

使查詢的字段名顯示為指定的名稱,使用 as 關(guān)鍵字

例:select Concat(vend_id,'(', vend_name,')') as vend_title from products      //字段名為 vend_title
最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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