如何在两个SELECT语句之间的SQL Server中执行LEFT JOIN?

时间:2022-10-19 09:39:39

I have two SELECT statements in SQL Server like these:

我在SQL Server中有两个SELECT语句,如下所示:

(SELECT [UserID] FROM [User])
(SELECT [TailUser], [Weight] FROM [Edge] WHERE [HeadUser] = 5043)

I want to perform a LEFT JOIN between these two SELECT statements on [UserID] attribute and [TailUser] attribute. I want to join existent records in second query with the corresponding records in first query and NULL value for absent records. How can I do this?

我想在[UserID]属性和[TailUser]属性上的这两个SELECT语句之间执行LEFT JOIN。我希望将第二个查询中的现有记录与第一个查询中的相应记录和缺失记录的NULL值相结合。我怎样才能做到这一点?

4 个解决方案

#1


43  

SELECT * FROM 
(SELECT [UserID] FROM [User]) a
LEFT JOIN (SELECT [TailUser], [Weight] FROM [Edge] WHERE [HeadUser] = 5043) b
ON a.UserId = b.TailUser

#2


1  

SELECT [UserID] FROM [User] u LEFT JOIN (
SELECT [TailUser], [Weight] FROM [Edge] WHERE [HeadUser] = 5043) t on t.TailUser=u.USerID

#3


1  

select *
from user
left join edge
on user.userid = edge.tailuser
and edge.headuser = 5043

#4


0  

Try this:

尝试这个:

SELECT user.userID, edge.TailUser, edge.Weight 
FROM user
LEFT JOIN edge ON edge.HeadUser = User.UserID
WHERE edge.HeadUser=5043

OR

要么

AND edge.HeadUser=5043

instead of WHERE clausule.

而不是WHERE clausule。

#1


43  

SELECT * FROM 
(SELECT [UserID] FROM [User]) a
LEFT JOIN (SELECT [TailUser], [Weight] FROM [Edge] WHERE [HeadUser] = 5043) b
ON a.UserId = b.TailUser

#2


1  

SELECT [UserID] FROM [User] u LEFT JOIN (
SELECT [TailUser], [Weight] FROM [Edge] WHERE [HeadUser] = 5043) t on t.TailUser=u.USerID

#3


1  

select *
from user
left join edge
on user.userid = edge.tailuser
and edge.headuser = 5043

#4


0  

Try this:

尝试这个:

SELECT user.userID, edge.TailUser, edge.Weight 
FROM user
LEFT JOIN edge ON edge.HeadUser = User.UserID
WHERE edge.HeadUser=5043

OR

要么

AND edge.HeadUser=5043

instead of WHERE clausule.

而不是WHERE clausule。