1. 程式人生 > >Mysql查詢語句——多表關聯查詢、子查詢

Mysql查詢語句——多表關聯查詢、子查詢

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

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

3.where條件查詢:select 欄位1,欄位2,欄位3 frome 表名 where 條件表示式;

例:select * from t_studect where id=1;

       select * from t_student where age>22;

4.帶in關鍵字查詢: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 ‘字串’;

    “%”代表任意字元;

    “_”代表單個字元;

例: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 條件表示式1 and 條件表示式2 [and 條件表示式n]

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

9.帶or的多條件查詢

select 欄位1,欄位2…frome 表名 where 條件表示式1 or 條件表示式2 [or 條件表示式n]

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

10.distinct去重複查詢:select distinct 欄位名 from 表名;

11.對查詢結果排序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預設可以不寫

12.分組查詢group by

group by 屬性名 [having 條件表示式][with rollup]

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

2.與group_concat()函式一起使用;

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

3.與聚合函式一起使用;

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

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

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

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

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

13.limit 分頁查詢:select 欄位1,欄位2,…from 表名 limit 初始位置,記錄數;

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

             多表連線查詢

select * from t_book,t_bookType;

1.內連線查詢(兩張或以上的表連線起來查詢需要的資料)

根據表一的bookTypeId查詢出所有bookTypeName

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

查詢某幾個欄位:

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

2.外連線查詢(兩張或以上的表連線起來查詢某張表的資訊)

3.左連線查詢

select * from t_book left join t_bookType on t_book.bookTypeId=t_bookType.id;

如下圖:表一(左邊表)t_book的資料全部查出 表二沒有的欄位用null代替

4.右連線查詢

select * from t_book right join t_bookType on t_book.bookTypeId=t_bookType.id;

查出表二(右邊表)的所有資訊,表一沒有的用null代替

5.多條件連線查詢

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

子查詢

1.帶in關鍵字的子查詢(一個查詢語句的條件可能落在另一個select語句的查詢結果中)

select * from t_book where bookType in(select id from t_bookType);

select * from t_book where bookType not in(select id from t_bookType);

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

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

3.帶exists關鍵字的子查詢(加入子查詢查詢到記錄,則進行外層查詢,否則,不執行外層查詢)

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

select * from t_book where not exists(select * from t_booktype);

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

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

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

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

合併查詢

1.union

使用union關鍵字是,資料庫系統會將所有的查詢結果合併到一起,然後去掉相同的記錄;

select id from t_book union select id from t_bookType;

2.union all

使用union all,不會去除掉重複的記錄;

select id from t_book union all select id from t_bookType;