mysql:mysql查詢語(yǔ)句 和 多表關(guān)聯(lián)查詢 以及 子查詢 章

1.查詢一張表:?? ??select * from 表名;

2.查詢指定字段:select 字段1,字段2,字段3….from 表名;

3.where條件查詢:select字段1,字段2,字段3 frome 表名 where 條件表達(dá)式;

例:select * from t_studect where id=1;

? ??? ?select * from t_student where age>22;

4.帶in關(guān)鍵字查詢:select?字段1,字段2 frome 表名 where 字段 [not]in(元素1,元素2);

例:select * from t_student where age in (21,23);

? ??? ?select * from t_student where age not in (21,23);

5.帶between and的范圍查詢:select?字段1,字段2 frome 表名 where 字段 [not]between 取值1 and 取值2;

例:select * frome t_student where age between 21 and 29;

? ? ? ?select * frome t_student where age not between 21 and 29;

6.帶like的模糊查詢:select?字段1,字段2… frome 表名 where 字段 [not] like ‘字符串’;

? ? “%”代表任意字符;

? ? “_”代表單個(gè)字符;

例:select * frome t_student where stuName like ‘張三”;

? ??? ?select * frome t_student where stuName like ‘張三%”;

?? ????select * frome t_student where stuName like ‘%張三%”;//含有張三的任意字符

? ??? ?select * frome t_student where stuName like ‘張三_”

7.空值查詢:select?字段1,字段2…frome 表名 where 字段 ?is[not] null;

8.帶and多條件查詢:

select?字段1,字段2…frome 表名 where 條件表達(dá)式1 and 條件表達(dá)式2 [and 條件表達(dá)式n]

例:select * frome t_student where gradeName=’一年級(jí)’ and age=23;

9.帶or的多條件查詢

select?字段1,字段2…frome 表名 where 條件表達(dá)式1 or 條件表達(dá)式2 [or 條件表達(dá)式n]

例:select * frome t_student where gradeName=’一年級(jí)’ or age=23;//或者,條件只要滿足一個(gè)

10.distinct去重復(fù)查詢:select distinct 字段名 from 表名;

11.對(duì)查詢結(jié)果排序order by:select 字段1,字段2…from 表名 order by 屬性名 [asc|desc]

例:select * frome t_student order by age desc;//降序,從大到小

? ??? ?select * frome t_student order by age asc;//升序,asc默認(rèn)可以不寫

12.分組查詢group by

group by 屬性名 [having 條件表達(dá)式][with rollup]

1.單獨(dú)使用(毫無(wú)意義,不能單獨(dú)使用);

2.與group_concat()函數(shù)一起使用;

例:select gradeName,group_concat(stuName) from t_student group by gradeName;

3.與聚合函數(shù)一起使用;

例:select gradeName,count(stuName) from t_student group by gradeName;

4.與having一起使用(顯示輸出的結(jié)果);

例:select gradeName,count(stuName) from t_student group by gradeName having count(stuName)>3;

5.與with rollup 一起使用(最后加入一個(gè)總和行);

例:select gradeName,group_concat(stuName) from t_student group by gradeName with rollup;


13.limit 分頁(yè)查詢:select 字段1,字段2,…from 表名 limit 初始位置,記錄數(shù);

例子:select * from t_student limit 0,5;

? ??? ??? ???多表連接查詢

表一:t_book

表二:t_bookType

表三:t_priceLevel

select * from t_book,t_bookType;

1.內(nèi)連接查詢(兩張或以上的表連接起來(lái)查詢需要的數(shù)據(jù))

根據(jù)表一的bookTypeId查詢出所有bookTypeName

select * from t_book,t_bookType where t_book.bookTypeId=t_bookType.id;

查詢某幾個(gè)字段:

select bookNme,author from t_book,t_bookType where t_book.bookTypeId=t_bookType.id;

2.外連接查詢(兩張或以上的表連接起來(lái)查詢某張表的信息)

3.左連接查詢

select * from t_bookleftjoint_bookTypeont_book.bookTypeId=t_bookType.id;

如下圖:表一(左邊表)t_book的數(shù)據(jù)全部查出 表二沒(méi)有的字段用null代替

4.右連接查詢

select * from t_bookrightjoint_bookTypeont_book.bookTypeId=t_bookType.id;

查出表二(右邊表)的所有信息,表一沒(méi)有的用null代替

5.多條件連接查詢

select * from t_book,t_bookType where t_book.bookTypeId=t_bookType.idandt_book.price>70;

子查詢

1.帶in關(guān)鍵字的子查詢(一個(gè)查詢語(yǔ)句的條件可能落在另一個(gè)select語(yǔ)句的查詢結(jié)果中)

select * from t_book where bookTypein(select id from t_bookType);

select * from t_book where bookTypenotin(select id from t_bookType);

2.帶比較運(yùn)算符的子查詢(子查詢可以使用比較運(yùn)算符)

select * from t_book where price>=(select price from t_priceLevel where priceLevel=1);

3.帶exists關(guān)鍵字的子查詢(加入子查詢查詢到記錄,則進(jìn)行外層查詢,否則,不執(zhí)行外層查詢)

select * from t_book whereexists(select * from t_booktype);

select * from t_book wherenot exists(select * from t_booktype);

4.帶any關(guān)鍵字的子查詢(any關(guān)鍵字表示滿足其中任一條件)

select * from t_book where price>=any(select price from t_priceLevel);

5.帶all關(guān)鍵字的子查詢(all關(guān)鍵字表示滿足所有條件)

select * from t_book where price>=all(select price from t_priceLevel);

合并查詢

1.union

使用union關(guān)鍵字是,數(shù)據(jù)庫(kù)系統(tǒng)會(huì)將所有的查詢結(jié)果合并到一起,然后去掉相同的記錄;

select id from t_book?union?select id from t_bookType;

2.union all

使用union all,不會(huì)去除掉重復(fù)的記錄;

select id from t_book?union allselect id from t_bookType;

摘自https://blog.csdn.net/github_37767025/article/details/67636061

?著作權(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)容

  • MYSQL 基礎(chǔ)知識(shí) 1 MySQL數(shù)據(jù)庫(kù)概要 2 簡(jiǎn)單MySQL環(huán)境 3 數(shù)據(jù)的存儲(chǔ)和獲取 4 MySQL基本操...
    Kingtester閱讀 8,071評(píng)論 5 115
  • 觀其大綱 page 01 基礎(chǔ)知識(shí) 1 MySQL數(shù)據(jù)庫(kù)概要 2 簡(jiǎn)單MySQL環(huán)境 3 數(shù)據(jù)的存儲(chǔ)和獲取 4 M...
    周少言閱讀 3,255評(píng)論 0 33
  • 朋友,今天我想說(shuō)一些自己難忘又感動(dòng)故事,我們生活中一定有那么一個(gè)人或者一件事讓我們久久不能忘記,或者說(shuō)不想...
    格諾閱讀 462評(píng)論 1 2
  • 斷了聯(lián)系的兩年,我過(guò)著平淡的生活,而你,開(kāi)始了你的新戀情,也可以說(shuō)是初戀吧,畢竟當(dāng)時(shí)的我們只是聊聊天而已,從朋友的...
    almostlover惜之閱讀 945評(píng)論 9 2
  • 上個(gè)月忽然家人住院耽誤了抄寫老師的銷售信,家人出院后總覺(jué)得有一件未完成任務(wù)等著我,可見(jiàn)老師的銷售信的威力已打入我心...
    羅土嗲閱讀 267評(píng)論 0 0

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