简体   繁体   English

使用基于外键的左连接仅检索一行

[英]Retrieve only one row using left join based on foreign key

I have 2 relationship tables in the database.我在数据库中有 2 个关系表。

One is Class一种是Class

create table Class(
    CourseId int,
    CourseName varchar(50),
    LecturerId int,
    Class1Time  varchar(50),
    Class2Time varchar(50),
    Class1Place varchar(50),
    Class2Place varchar(50)
);

and LecturerLecturer

create table Lecturer(
    Id int,
    FirstName varchar(50),
    LastName varchar(50),
    primary key(Id)
);

LecturerId from Class table is foreign key of Id from Lecturer . Class表中的LecturerIdLecturerId的外键。 All I want is to select the data(one row) from both table based on the foreign key LecturerId = specefic value我想要的只是 select 基于外键LecturerId = specefic value的两个表中的数据(一行)

I've tried using left join like我试过使用左连接

select Class.CourseName, Class.CourseId, Class.Class1Time, Class.Class2Time, Class.Class1Place, Class.Class2Place, Lecturer.Id, Lecturer.FirstName, Lecturer.LastName
from Class 
left join Lecturer 
    on Lecturer.Id = (select Lecturer.Id from Lecturer where LecturerId = Class.LecturerId)
    where Class.LecturerId = someValue;
go 

But it causes the error:但这会导致错误:

Subquery returned more than 1 value.子查询返回超过 1 个值。 This is not permitted when the subquery follows =, ,=, <, <=, >.当子查询跟随 =, ,=, <, <=, > 时,这是不允许的。 >= or when the subquery is used as an expression. >= 或当子查询用作表达式时。

For example in the Lecturer table I have a Id = 2001例如,在Lecturer表中,我有一个Id = 2001

So I want perform the above query that need to return the below result like所以我想执行上面的查询,需要返回下面的结果,比如

CourseName - CourseId - Class1Time - Class2Time - Class1Place - Class2Place - Lecturer Infor(based on the Id)

Just only one row.只有一排。

Try this, using table aliases, with all columns aliased.试试这个,使用表别名,所有列都有别名。

select C.CourseName, C.CourseId, C.Class1Time, C.Class2Time, C.Class1Place, C.Class2Place, L.Id, L.FirstName, L.LastName
from Class C
left join Lecturer L on L.Id = C.LecturerId
where C.LecturerId = @Id;

The reason you get that error is because:您收到该错误的原因是:

(select Lecturer.Id from Lecturer where LecturerId = Class.LecturerId)

Is actually the same as:实际上是一样的:

(select Lecturer.Id from Lecturer where Class.LecturerId = Class.LecturerId)

Because Lecturer doesn't have a column called LecturerId , and because you haven't table qualified the column, SQL Server realises it does exist in Class and therefore uses it.因为Lecturer没有名为LecturerId的列,并且因为您没有对该列进行表格限定,所以 SQL 服务器意识到它确实存在于Class中,因此使用它。

And since Class.LecturerId = Class.LecturerId is true for all records in the 'Lecturer' table you get all its ids returned.由于Class.LecturerId = Class.LecturerId对于“Lecturer”表中的所有记录都为真,因此您将返回其所有 id。

And of course there is actually no reason to perform a subquery anyway, because you already have the value you are using the subquery to obtain.当然,实际上没有理由执行子查询,因为您已经拥有使用子查询获取的值。

Try the following please.请尝试以下方法。

DECLARE @LecturerId INT = 1

SELECT C.CourseName, C.CourseId, C.Class1Time, C.Class2Time, C.Class1Place, C.Class2Place, L.Id, L.FirstName, L.LastName
FROM Class AS C 
INNER JOIN Lecturer AS L ON L.Id = C.LecturerId
INNER JOIN
(
    SELECT C.CourseId, C.LecturerId
    FROM Class AS C     
    WHERE C.LecturerId = @LecturerId
    GROUP BY C.CourseId, C.LecturerId
) AS G ON G.CourseId = C.CourseId AND G.LecturerId = C.LecturerId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM