[英]SQL how to select all rows from table joining other table even if there are no rows associated
[英]Joining a sql table rows with all other rows of same table
使用CROSS JOIN
,这将为您提供两个表之间的笛卡尔积
Select *
From YourTable A
CROSS JOIN YourTable B
Where A.ReqStatusId <> B.ReqStatusId
您想要得到的是通过cross join
实现的。 如果您两次选择该表,则将获得所需的结果。
select a.reqstatusid, a.reqstatus, b.reqstatusid as childreqstatusid,
b.reqstatus as childreqstatus
from table a, table b
where a.reqstatusid <> b.reqstatusid
您应该对ReqStatusId <> ReqStatusId
进行JOIN
:
WITH Tbl(ReqStatusId, ReqStatus) AS(
SELECT 1, 'Open' UNION ALL
SELECT 2, 'On Hold' UNION ALL
SELECT 3, 'Closed'
)
SELECT
t1.*,
ChildId = t2.ReqStatusId,
ChildReqStatus = t2.ReqStatus
FROM Tbl t1
INNER JOIN Tbl t2
ON t2.ReqStatusId <> t1.ReqStatusId
select a.reqstatusid, a.reqstatus, b.reqstatusid,
b.reqstatus
from table a
inner join table b on A.ReqStatusId = B.ReqStatusId
Where A.ReqStatusId <> B.ReqStatusId
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.