首页 > 代码库 > SQL连接 自我学习,跑完秒懂

SQL连接 自我学习,跑完秒懂

--DROP TABLE dbo.TableA;
--DROP TABLE dbo.TableB;
--CREATE TABLE TableA
--(
-- id INT PRIMARY KEY IDENTITY(1,1),
-- nm nvarchar(20)
--);

--CREATE TABLE TableB
--(
-- id INT PRIMARY KEY IDENTITY(1,1),
-- job int,
-- aid INT
--);

SELECT * FROM dbo.TableA;
SELECT * FROM dbo.TableB;
--INNER JOIN
SELECT dbo.TableA.*, dbo.TableB.* FROM dbo.TableA INNER JOIN dbo.TableB on dbo.TableA.id = dbo.TableB.aid;

--LEFT join
SELECT dbo.TableA.*, dbo.TableB.* FROM dbo.TableA LEFT JOIN dbo.TableB ON TableA.id = TableB.id;

--RIGHT JOIN
SELECT dbo.TableA.*, dbo.TableB.* FROM dbo.TableA Right JOIN dbo.TableB ON TableA.id = TableB.aid;

--FULL JOIN
SELECT dbo.TableA.*, dbo.TableB.* FROM dbo.TableA FULL JOIN dbo.TableB ON TableB.aid = TableA.id;

--UNION
SELECT dbo.TableA.id FROM dbo.TableA UNION SELECT dbo.TableB.aid FROM dbo.TableB

--UNION ALL
SELECT dbo.TableA.id FROM dbo.TableA UNION all SELECT dbo.TableB.aid FROM dbo.TableB

SQL连接 自我学习,跑完秒懂