您好,登錄后才能下訂單哦!
SQL Server中怎么實現多表連接查詢功能 ,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
具體代碼如下所示:
-- 交叉連接產生笛卡爾值 (X*Y)SELECT *FROM Student cross Join dbo.ClassInfo --另外一種寫法SELECT *FROM Student , ClassInfo -- 內連接 (Inner 可以省略)SELECT *FROM Student JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID;-- Inner JoinSELECT *FROM Student INNER JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID; -- on 條件,通常是 主外鍵,但是不限于主外鍵 -- on 條件,允許有多個,而且可能是針對某個表的SELECT *FROM Student INNER JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID AND dbo.Student.Class IN (1,2) -- 針對Student表增加查詢條件-- 不等于 (笛卡爾值減去 相等的值)SELECT *FROM Student INNER JOIN dbo.ClassInfo ON dbo.Student.Class <> dbo.ClassInfo.ID;--自連接 從Class表中查詢到Class所在的系SELECT T1.* ,T2.ClassName FROM dbo.ClassInfo AS T1 INNER JOIN dbo.ClassInfo AS T2 ON T1.PID=T2.ID-- Left JoinSELECT *FROM Student Left JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID;-- Right JoinSELECT *FROM Student RIGHT JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID;-- 全外連接SELECT *FROM Student FULL OUTER JOIN dbo.ClassInfo ON dbo.Student.Class = dbo.ClassInfo.ID;-- 外連接和內連接的區別是:-- 內連接,on條件不符合的話,會過濾掉數據-- 外連接,以保留表為主,on條件成立顯示數據,否則顯示NULL--Union 縱向連接SELECT StuID,StuName,StuEnName,StuAge,StuBirthdayFROM Student WHERE StuID<=2UNIONSELECT StuID,StuName,StuEnName,StuAge,StuBirthdayFROM dbo.Student WHERE StuID>2--Union 去重復SELECT StuSexFROM Student WHERE StuID<=2UNIONSELECT StuSexFROM dbo.Student WHERE StuID>2--Union 顯示全部SELECT StuSexFROM Student WHERE StuID<=2UNION ALLSELECT StuSexFROM dbo.Student WHERE StuID>2--Except 差集,排除SELECT StuID,StuName,StuEnName,StuAge,StuBirthdayFROM StudentExceptSELECT StuID,StuName,StuEnName,StuAge,StuBirthdayFROM dbo.Student WHERE StuID<=2--Intersect 交集SELECT StuID,StuName,StuEnName,StuAge,StuBirthdayFROM Student WHERE StuID>=2ExceptSELECT StuID,StuName,StuEnName,StuAge,StuBirthdayFROM dbo.Student WHERE StuID<=3
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。