mysql相關查詢 MySQL Join相關查詢

2021-10-18 11:48:38 字數 3839 閱讀 9097

前言

有時為了得到完整的結果,我們需要從兩個或者更多的表中獲取資料,這時join就派上用場了。它可以基於這些表之間的共同欄位把表結合起來。它有幾種形式,分別為:join、inner join、left join、right join等等。其中join和inner join相同,這裡以join的用法來代表兩者。

資料來源create table `student` ( `id` int(10) not null auto_increment comment '主鍵id', `sname` varchar(10) default null comment '學生姓名', `sno` int(11) default null comment '學號', `school_id` int(10) default null comment '學校id', primary key (`id`) ) engine=innodb auto_increment=4 default charset=utf8;

create table `school` ( `id` int(10) not null auto_increment comment '主鍵id', `school_name` varchar(10) default null comment '學校名稱', primary key (`id`) ) engine=innodb auto_increment=4 default charset=utf8;

insert into `student` (`id`, `sname`, `sno`, `school_id`) values ('1', '琚建飛', '130', '1');

insert into `student` (`id`, `sname`, `sno`, `school_id`) values ('2', '張三', '131', '2');

insert into `student` (`id`, `sname`, `sno`, `school_id`) values ('3', '李四', '132', '3');

insert into `school` (`id`, `school_name`) values ('1', '廊坊師範');

insert into `school` (`id`, `school_name`) values ('2', '清華大學');

insert into `school` (`id`, `school_name`) values ('3', '北京大學');

join

join會返回匹配的所有行。

mysql> select s.sname,sc.school_name from student s join school sc on s.school_id = sc.id; +--------+-------------+

| sname | school_name | +--------+-------------+

| 琚建飛 | 廊坊師範 |

| 張三 | 清華大學 |

| 李四 | 北京大學 | +--------+-------------+

3 rows in set

left join

left join會返回左表中的所有行,即使在右表中無匹配行。

現在,把student表中,張三的school_id改為4,李四的school_id改為5,示例**:

//join表的效果

mysql> select s.sname,sc.school_name from student s join school sc on s.school_id = sc.id; +--------+-------------+

| sname | school_name | +--------+-------------+

| 琚建飛 | 廊坊師範 | +--------+-------------+

1 row in set

//left join的效果

mysql> select s.sname,sc.school_name from student s left join school sc on s.school_id = sc.id; +--------+-------------+

| sname | school_name | +--------+-------------+

| 琚建飛 | 廊坊師範 |

| 張三 | null |

| 李四 | null | +--------+-------------+

3 rows in set

right join

right join關鍵字會返回在右表所有的行,即使在左表中沒有匹配的行。

mysql> select s.sname,sc.school_name from student s right join school sc on s.school_id = sc.id; +--------+-------------+

| sname | school_name | +--------+-------------+

| 琚建飛 | 廊坊師範 |

| null | 清華大學 |

| null | 北京大學 | +--------+-------------+

3 rows in set

union

列出所有連線表的全部資料。mysql不支援full outer join關鍵字。替代格式為:

mysql> select * from student s left join school sc on s.school_id = sc.id

union

select * from student s right join school sc on s.school_id = sc.id;

| id | sname | sno | school_id | id | school_name |

| 1 | 琚建飛 | 130 | 1 | 1 | 廊坊師範 |

| 2 | 張三 | 131 | 4 | null | null |

| 3 | 李四 | 132 | 5 | null | null |

| null | null | null | null | 2 | 清華大學 |

| null | null | null | null | 3 | 北京大學 |

5 rows in set

union 操作符用於合併兩個或多個 select 語句的結果集。語法格式為:

select column_name(s) from table_name1 union select column_name(s) from table_name2

預設地,union 操作符選取不同的值。如果允許重複的值,請使用 union all。

//在student表新增一行資料

insert into student(sname,sno,school_id) values('王五',130,1);//使用union

mysql> select school_id from student union select id from school; +-----------+ | school_id |

| 4 |

| 5 |

| 1 |

| 2 |

| 3 | +-----------+

5 rows in set

//使用union all

mysql> select school_id from student union all select id from school; +-----------+ | school_id |

| 4 |

| 5 |

| 1 |

| 1 |

| 2 |

| 3 | +-----------+

6 rows in set

mysql 慢查詢相關

1 如何查詢慢查詢是否開啟 2 伺服器端如何設定 路徑 data mysql db 3306 conf 設定值如下 其中 general log 是指常規日誌的,預設是off,如果要有除錯的需要,是可以暫時開啟。long query time 單位是秒?log queries not using i...

MySQL 時間相關查詢

from unixtime 函式可以將mysql中以int型別儲存的時間 自1970年1月1日0時0分0秒到指定時間所經過的秒數 用一種指定的格式來顯示,如未指定格式則預設以年月日時分秒的形式返回。from unixtime unix timestamp,format 表示返回unix時間標記的乙個...

MySQL 日期相關查詢

select from 表名 where to days 時間欄位名 to days now select from 表名 where to days now to days 時間欄位名 1 select from 表名 where date sub curdate interval 7 day d...