简体   繁体   中英

Retrieve only one row using left join based on foreign key

I have 2 relationship tables in the database.

One is Class

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

and Lecturer

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 . All I want is to select the data(one row) from both table based on the foreign key 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. 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

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.

And since Class.LecturerId = Class.LecturerId is true for all records in the 'Lecturer' table you get all its ids returned.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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