USE [Test]
GO
--创建表--
CREATE TABLE ClassDemo
(
ClassID UNIQUEIDENTIFIER PRIMARY KEY,
ClassName NVARCHAR(200)
)
CREATE TABLE StudentDemo
(
StudentID UNIQUEIDENTIFIER PRIMARY KEY,
StudentName NVARCHAR(200),
ClassID UNIQUEIDENTIFIER
)
--插入数据--
SELECT NEWID()
--DELETE FROM ClassDemo
--DELETE FROM StudentDemo
INSERT INTO ClassDemo(ClassID,ClassName)
VALUES('792886C9-4543-4209-9DFA-7BC7DE69C4B6','班级1');
INSERT INTO ClassDemo(ClassID,ClassName)
VALUES('F0A8CF54-E84D-4B46-8D46-DCFEFBA1D3D2','班级2');
INSERT INTO StudentDemo(StudentID,StudentName,ClassID)
VALUES('71C92D0B-17A0-4B92-954A-1566852E1E63','学生1','792886C9-4543-4209-9DFA-7BC7DE69C4B6');
INSERT INTO StudentDemo(StudentID,StudentName,ClassID)
VALUES('EAA4AC47-5E17-41CE-9791-0DE59C7CCBAD','学生2','F0A8CF54-E84D-4B46-8D46-DCFEFBA1D3D2');
--查询--
SELECT * FROM ClassDemo c
LEFT JOIN StudentDemo s ON c.ClassID=s.ClassID AND s.StudentID='EAA4AC47-5E17-41CE-9791-0DE59C7CCBAD'
SELECT * FROM ClassDemo c
LEFT JOIN StudentDemo s ON c.ClassID=s.ClassID
WHERE s.StudentID='EAA4AC47-5E17-41CE-9791-0DE59C7CCBAD'
第一条查询语句的查询结果:先过滤StudentDemo数据,然后在和ClassDemo左联
第二条查询语句的查询结果:StudentDemo先和ClassDemo左边,然后在筛选数据