MySQL知識要點7 高階查詢

2021-09-11 06:03:34 字數 1453 閱讀 2788

高階查詢

主要分為:關聯查詢子查詢聯合查詢

關聯查詢

主要包括內連線、外連線、自然連線、自連線

1

. 內連線

通用列欄位名稱必須一致,去除重複字段

關聯表**現的字段值最終才能出現在結果集中

內連線與連線順序無關,沒有主從表之分

select name, score from student,

class

where student.class_name =

class

.class_name;

select *

from student inner join class

on student.class_name =

class

.class_name;

select *

from student inner join class

using

(class_name);2

. 外連線

外連線有主從表之分,與連線循序有關

依次遍歷主表中記錄,與從表中記錄進行匹配,如果匹配到則連線展示,否則以null填充

select *

from student left join class

on student_name=class_name;

# 全部展示左邊student內的資料

select *

from student right join class

on student_name=class_name;

# 全部展示右邊student內的資料

select *

from student outer join class

on student_name=class_name;

# 只顯示匹配到的兩表資料

子查詢

主要包括:單行子查詢、多行子查詢等

# 單行子查詢

select *

from student where score>

(select score from student where name=

'張三');

# 多行子查詢

select *

from student where score in

(select score from student where class_id=3)

and name like '張%'

;

聯合查詢

union 將結果進行整合到乙個表中

select name from student_score

union

select name from student_name;

MySQL高階知識 Join查詢

前言 該篇主要對mysql中join語句的七種情況進行總結。join主要根據兩表或多表之間列的關係,從這些表中進行資料的查詢。1.tb emp表。drop table if exists tb emp create table tb emp id int 11 not null auto incre...

MySQL高階知識1 多表查詢

當我們對多表進行聯查時,我們可以得到 表1 表2 的記錄數。例子 表1的 name 欄位和表2的 name 字段關聯 select from t1,t2 where t1.name t2.username 補充 如果兩個欄位不一樣,也可不帶表名作為字首。直接通過例子來看 例子 同一張表,查詢員工對應...

MySQL高階知識(二) Join查詢

前言 該篇主要對mysql中join語句的七種情況進行總結。join主要根據兩表或多表之間列的關係,從這些表中進行資料的查詢。1.tb emp表。drop table if exists tb emp create table tb emp id int 11 not null auto incre...