MySQL資料庫多表查詢

2021-10-23 08:37:11 字數 2203 閱讀 3139

多表查詢:多個有關係的表關聯查詢。

#user_info表

create

table user_info(

id int(2

)primary

key,

user_name varchar(12

)unique

, password varchar(15

)not

null

, real_name varchar(8

)not

null

, age int(3

));#address表

create

table address(

id int(2

)primary

key,

user_id int(2

)not

null

, real_name varchar(8

),mobile char(11

),address varchar

(150))

;insert

into user_info values(1

,'淺唱灬幸福'

,'8912@321'

,'王曉明',12

);insert

into address values(1

,1,'王小明'

,'15516472282'

,'山西太原');

insert

into address values(2

,1,'王鑫'

,'18404905139'

,'山西大同');

insert

into address values(3

,1,'任建'

,'15333021730'

,'山西晉城');

insert

into user_info values(2

,'ぅ浮生若夢〤'

,'56701wz'

,'王楠',36

);insert

into address values(4

,2,'王楠'

,'15010303314'

,'北京海淀');

insert

into address values(5

,2,'趙婕'

,'18435224278'

,'山西長治');

insert

into user_info values(3

,'街角の風鈴'

,'27w4921'

,'李曉飛',9

);insert

into address values(6

,6,'劉倩'

,'13159775555'

,'吉林長春'

);

定義:取多表之間交集,用=判斷值是否相等。

select ui.

*,addr.

*from user_info ui,address addr where ui.id=addr.user_id;

顯示出兩個表id相等的資訊:

左連線:查詢出left關鍵字左邊表的所有資料,與on後的條件無關。

右連線:查詢出right關鍵字右邊表的所有資料,與on後的條件無關。

資料庫 MySQL多表查詢

某教學資料結構大概如下 表1 student 學生表 sid,name,age,gender表2 course 課程表 cid,name,teacher表3 score 成績表 sid,cid,score 成績 編寫sql語句查詢所有學習課程名為python的學生資訊,實現語句如下 select s...

資料庫的多表查詢 mysql

dml sql語句的乙個分類,dml主要完成資料庫表中資料的維護,即 新增 刪除 修改 實體完整性 表中不能出現兩行完全一樣的資料 解決方案 給表中新增主鍵 id 讓該列的值唯一 域完整性 表中的值必須正確,在mysql8.0之後,可以使用check關鍵字實現 引用完整性 自定義完整性 需要根據業務...

MySQL資料庫 多表連線查詢

多表連線查詢 注意 使用連線技術建議將表經行重新命名!explain 檢索連線是否達標 內連線 語法1 from 表1 inner join 表2 on 主鍵字段 外來鍵字段 where 條件表示式 語法2 from 表1,表2 where 主鍵字段 外來鍵字段 and 條件表示式 三個表連線 fr...