mysql – 在两列中组合两个select语句?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mysql – 在两列中组合两个select语句?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1660字,纯文字阅读大概需要3分钟。
内容图文
![mysql – 在两列中组合两个select语句?](/upload/InfoBanner/zyjiaocheng/893/16c2b475e8e54b0d9dc83d3ed20e43e7.jpg)
我有两个选择声明
1
select Start_Date
from table1 where Start_Date not in (
select End_Date
from table1)
2
select End_Date from table1
where End_Date not in (
select Start_Date
from table1
)
我想在我使用union时将不同列中的两个select语句组合在一起它给我一个列,其中两个查询的结果我希望每个查询结果在不同的列中但是当我使用内部联接时
select a.End_Date , b.Start_Date from
( select End_Date from table1
where End_Date not in (
select Start_Date
from table1
) ) a
join
(select Start_Date
from table1 where Start_Date not in (
select End_Date
from table1)
) b
on 1=1
它给我的结果每个记录重复四次帮助我下一步做什么?
解决方法:
如果您的每个查询只返回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)
内容总结
以上是互联网集市为您收集整理的mysql – 在两列中组合两个select语句?全部内容,希望文章能够帮你解决mysql – 在两列中组合两个select语句?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。