如果您的每个查询只返回1行,您可以使用:
SELECT
(select Start_Date
from table1 where Start_Date not in (
select End_Date
from table1)
) AS StartDate,
(select End_Date from table1
where End_Date not in (
select Start_Date
from table1)
) AS EndDate
如果您的查询返回超过1行,则必须选择其他解决方案:
你可以使用UNION:
(您将在另一列中将两个查询与“NULL”错位)
(select Start_Date, Null AS EndDate
from table1 where Start_Date not in (
select End_Date
from table1)
)
UNION
(select Null As StartDate, End_Date
from table1
where End_Date not in (
select Start_Date
from table1)
)
你可以使用JOIN
如果你有一个字段用作“Join On”你可以使用这个字段,如果没有你可以添加一个字段来加入(但你需要检查返回的数据以避免错误)
你还要检查什么样的连接可能对你有好处(内 – 左 – rigth)
在示例中,我添加了一个字段来加入并使用内部联接:
SELECT Start_Date, End_Date
FROM
(select 1 as InnerId, Start_Date
from table1 where Start_Date not in (
select End_Date
from table1)
) As Tab1
INNER JOIN
(select 1 as InnerId, End_Date from table1
where End_Date not in (
select Start_Date
from table1)
) AS Tab2
USING(InnerId)