SQL join關(guan)鍵字
如(ru)果一張表有很多個字段(duan)可能填入起(qi)來十分的(de)困(kun)難復雜,不如(ru)把它拆(chai)分成兩個表,然后查看的(de)時候合并(bing)起(qi)來。
比如我要記錄學生的姓名,班級,成績,父母的電話號碼,那么我們可以創建一個表1 儲存學生的姓名班級成績,表二儲存學生的父母的電話號碼
首先表(biao)1 叫student
create table student(
name varchar(20),
class varchar(20),
grade double
);
然后數據什么的自己隨便填點吧,方便測試。我這里就用現成的數據了。
這是填完之后的樣子

接下來我們創建表2 叫parent_information
CREATE TABLE parent_information (
name VARCHAR(20),
father_tel VARCHAR(20),
mather_tel VARCHAR(20)
);
然后我們select 一下

如果領(ling)導要求(qiu)我(wo)們匯總一下(xia)表的(de)話(hua)我(wo)們直接join一下(xia)就行
SELECT
student.name,
student.class,
parent_information.father_tel,
parent_information.mather_tel,
student.grade
FROM
student inner
JOIN
parent_information ON student.name = parent_information.mather_tel;

需要注意的(de)是,select并不會對本身表(biao)的(de)結構發生改變。
