mysql两列组合查询_mysql – 在两列中组合两个select语句?

如果您的每个查询只返回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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值