如果行为空,如何使用第二个 "ON" 条件将 table 与自身连接
How to join table with itself, with a second "ON" criteria, if a row is null
我有一个用户 table。其中,它有以下 4 列:
------ UserID
------ | ---- Username
----- | --- CreatedBy
--- | ParentUserID
(PK, bigint, not null) | (字符(20),不为空) | (varchar(50), null) | (bigint, null)
ParentUserID 和 CreatedBy 分别通过 UserID 或 Username 指向 "owner account"。两者都是独一无二的。
CreatedBy 实际上从未 null
,但 UserID 已编入索引,因此首选 ParentUserID - 这也是我们正在朝着的目标。
显然我并不流利SQL,但这是我的想法:
SELECT Users.*
FROM tblUsers AS Owners
LEFT JOIN tblUsers AS Users
ON
ISNULL(Users.ParentUserID = Owners.UserID,
Users.CreatedBy = Owners.Username)
WHERE Owners.UserID = 14;
据我所知:
SELECT ISNULL(POwners.UserID, COwners.UserID) AS OwnerID, Users.*
FROM tblUsers AS Users
RIGHT JOIN tblUsers AS POwners ON Users.ParentUserID = POwners.UserID
RIGHT JOIN tblUsers AS COwners ON Users.CreatedBy = COwners.Username
WHERE OwnerID = 14;
虽然显然这行不通。在次要说明中,我还需要将其转换为 LINQ,但对于这个问题,到目前为止只有查询可以转换才相关,这是我期望的绝大多数查询。
join
条件只是布尔测试,因此您需要编写适当的布尔条件,例如(P or Q) AND R
。你不能用 ,
链接它们,所以...
... ON ISNULL((Users.ParentUserID = Owners.UserID) AND (Users.CreatedBy = Owners.Username))
或者任何你需要的逻辑。使其成为 VALID 布尔表达式是关键部分。
这似乎是对我有用的查询:
SELECT Users.*
FROM tblUsers AS Owners
LEFT JOIN tblUsers AS Users
ON Users.ParentUserID = Owners.ParentUserID
OR Users.CreatedBy = Owners.Username
WHERE Owners.UserID = 14;
感谢 的帮助。
至于LINQ,原来是only equijoins are supported,所以我做了这样的交叉连接:
from u in dbContext.tblUsers
from o in dbContext.tblUsers
where (u.ParentUserID == o.UserID || u.CreatedBy == o.Username)
&& o.UserID == 14
select u;
这变成了以下查询:
SELECT Users.*
FROM tblUsers AS Users
CROSS JOIN tblUsers AS Owners
WHERE(Users.ParentUserID = Owners.UserID OR Users.CreatedBy = Owners.Username)
AND(Owners.UserID = 14)
我有一个用户 table。其中,它有以下 4 列:
------ UserID
------ | ---- Username
----- | --- CreatedBy
--- | ParentUserID
(PK, bigint, not null) | (字符(20),不为空) | (varchar(50), null) | (bigint, null)
ParentUserID 和 CreatedBy 分别通过 UserID 或 Username 指向 "owner account"。两者都是独一无二的。
CreatedBy 实际上从未 null
,但 UserID 已编入索引,因此首选 ParentUserID - 这也是我们正在朝着的目标。
显然我并不流利SQL,但这是我的想法:
SELECT Users.*
FROM tblUsers AS Owners
LEFT JOIN tblUsers AS Users
ON
ISNULL(Users.ParentUserID = Owners.UserID,
Users.CreatedBy = Owners.Username)
WHERE Owners.UserID = 14;
据我所知:
SELECT ISNULL(POwners.UserID, COwners.UserID) AS OwnerID, Users.*
FROM tblUsers AS Users
RIGHT JOIN tblUsers AS POwners ON Users.ParentUserID = POwners.UserID
RIGHT JOIN tblUsers AS COwners ON Users.CreatedBy = COwners.Username
WHERE OwnerID = 14;
虽然显然这行不通。在次要说明中,我还需要将其转换为 LINQ,但对于这个问题,到目前为止只有查询可以转换才相关,这是我期望的绝大多数查询。
join
条件只是布尔测试,因此您需要编写适当的布尔条件,例如(P or Q) AND R
。你不能用 ,
链接它们,所以...
... ON ISNULL((Users.ParentUserID = Owners.UserID) AND (Users.CreatedBy = Owners.Username))
或者任何你需要的逻辑。使其成为 VALID 布尔表达式是关键部分。
这似乎是对我有用的查询:
SELECT Users.*
FROM tblUsers AS Owners
LEFT JOIN tblUsers AS Users
ON Users.ParentUserID = Owners.ParentUserID
OR Users.CreatedBy = Owners.Username
WHERE Owners.UserID = 14;
感谢
至于LINQ,原来是only equijoins are supported,所以我做了这样的交叉连接:
from u in dbContext.tblUsers
from o in dbContext.tblUsers
where (u.ParentUserID == o.UserID || u.CreatedBy == o.Username)
&& o.UserID == 14
select u;
这变成了以下查询:
SELECT Users.*
FROM tblUsers AS Users
CROSS JOIN tblUsers AS Owners
WHERE(Users.ParentUserID = Owners.UserID OR Users.CreatedBy = Owners.Username)
AND(Owners.UserID = 14)