SQL 外連結操作小結

來源:互聯網
上載者:User

網上引用,具體作者不祥

簡介:
外部串連和自我聯結
inner join(等值串連) 只返回兩個表中連接欄位相等的行
left join(左聯結) 返回包括左表中的所有記錄和右表中連接欄位相等的記錄
right join(右聯結) 返回包括右表中的所有記錄和左表中連接欄位相等的記錄
on 指定表間連接欄位及其關係的等號 "=" 運算式, 返回 true 或 false. 當運算式返回 true 時, 則查詢中包含該記錄.
! 外部串連只能操作已存在於資料庫中的資料

Update (ctarticle AS a LEFT JOIN ctclass AS c ON a.classid = c.classid) LEFT JOIN cttag AS b ON a.articleid = b.articleid
SET tag=tag+' ', b.articleid=a.articleid, b.classid=a.classid, b.nclassid=a.nclassid
Where a.classid=23 AND a.nclassid=0 AND tagid is not null

Update (ctarticle AS a LEFT JOIN (ctnclass AS c LEFT JOIN ctclass AS d ON c.classid = d.classid) ON a.nclassid = c.nclassid AND a.classid = c.classid) LEFT JOIN cttag AS b ON a.articleid = b.articleid SET tag=d.class+' '+c.nclass, b.articleid=a.articleid, b.classid=a.classid, b.nclassid=a.nclassid Where a.classid=23 AND a.nclassid=197;

更新操作
左串連中資料的篩選
Insert INTO cttag(articleid,classid,nclassid) Select a.articleid,a.classid,a.nclassid from ctarticle a left join cttag b on a.articleid=b.articleid where b.articleid is null

//本語句功能為, 顯示主表的全部內容, 插入資料到副表中沒有的資料
//主要作用為: 讓資料減少冗餘

上例中的延續
Select a.*, b.*, c.*, d.*
FROM cttag as d left join ((ctarticle AS a LEFT JOIN ctclass AS b ON a.classid=b.classid) LEFT JOIN ctnclass AS c ON a.nclassid=c.nclassid) on d.articleid=a.articleid;

顯示文章表中的全部, 調用類別表中的欄目
select a.*, b.*, c.* from (ctarticle a left join ctclass b on a.classid=b.classid) left join ctnclass c on a.nclassid=c.nclassid

//作用, 有時在文章表中包含了在個別類別表中沒有的資料, 用這個文法可以讀出文章表的全部資料
//a 為 文章表, b 為主類別, c 為子類別

同上例, 選擇追加資料時加上空格
Insert INTO cttag(articleid,classid,nclassid,tag)
Select a.articleid,a.classid,a.nclassid,d.class+' '+c.nclass
FROM (ctarticle AS a left join (ctnclass c left join ctclass d on c.classid=d.classid) on a.classid=c.classid and a.nclassid=c.nclassid) LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=4 and a.nclassid=154;

串連N個表, 並追加資料到其中一個表, N=4
insert INTO cttag(articleid,classid,nclassid,tag)
Select a.articleid,a.classid,a.nclassid,d.class+c.nclass
FROM (ctarticle AS a left join (ctnclass c left join ctclass d on c.classid=d.classid) on a.classid=c.classid and a.nclassid=c.nclassid) LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=1 and a.nclassid=1;

//解讀
插入到 表2(欄1,欄2,欄3,欄4)
選擇 別名a.欄1, 別名a.欄2, 別名a.欄3, 別名d.欄4 加上 別名c.欄5
從 (表1 別名a 左串連 (表3 別名c 左串連 表4 別名d 在 別名c.欄2 等於 別名d.欄2) 在 別名a.欄2 等於 別名c.欄2 和 別名a.欄3=別名c.欄3) 左串連 表2 別名b 在 別名a.欄1 等於 別名b.欄1 在那裡 別名a.欄2=1 和 別名a.欄3=1

串連兩個表, 並追加資料到其中一個表
Insert INTO cttag(articleid,classid,nclassid)
Select a.articleid,a.classid,a.nclassid
FROM ctarticle AS a LEFT JOIN cttag AS b ON a.articleid = b.articleid where a.classid=1 and a.nclassid=1;

//解讀
插入到 表2(欄1,欄2,欄3)
選擇 別名a.欄1, 別名a.欄2, 別名a.欄3
從 表1 別名a 左串連 表2 別名b 在 別名a.欄1 等於 別名b.欄1 在那裡 別名a.欄4=1 和 別名a.欄5=1

左串連

同步兩表的資料
Update ctarticle a INNER JOIN cttag b ON a.articleid = b.articleid SET b.classid=a.classid, b.nclassid=a.nclassid;

//解讀
更新 表1 別名a 聯結 表2 別名2 在 別名a.欄1 等於 別名b.欄1 設定 別名b.欄2 更新為 別名a.欄2, 別名b.欄3 更新為 別名a.欄3

右外串連
select a.*, b.* from bunclass a right join ctclass b on a.classid=b.classid where a.nclassid=20

查詢別名 a,b 表, 只匹配 b 表中的內容.

添加資料到串連表之一
Insert INTO cttag ( tag, articleid ) Select top 1 b.tag, a.articleid FROM ctarticle AS a left JOIN cttag AS b ON a.articleid = b.articleid Where a.articleid order by a.articleid desc;

變通中的用法二
Insert INTO bureply
Select b.*, a.classid, a.nclassid
FROM article AS a INNER JOIN reply AS b ON a.articleid = b.articleid
Where classid=50;

實際應用中的變通
Insert INTO butag ( tag, articleid, classid, nclassid)
Select b.tag, a.articleid, a.classid, a.nclassid
FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
Where classid=24;

添加資料到其他表
Insert INTO butag ( tag, articleid )
Select b.tag, a.articleid
FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
Where a.articleid<>False;

//解讀
添加到 接收表(列1,列2)
選擇 別名b.列1, 別名a.列2
從 表1 表名a 聯結 表2 表名b 在 別名a.列c 等於 別名b.列c
在哪裡 別名a.列c 不等於 沒有

實際應用中的變通
Select b.tag, a.articleid, a.classid, a.nclassid
FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
Where a.classid=24;

查詢
Select b.tag, a.articleid
FROM article AS a INNER JOIN tag AS b ON a.articleid = b.articleid
Where a.articleid<>False;

//解讀
選擇 別名b.列, 別名a.列
從 表1 別名a 聯結 表2 別名b 在 別名a.列c = 別名b.列c
在哪裡 別名a.列c 不等於 沒有
注: as 不是必要

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.