![](/img/trans.png)
[英]ordering a union with two selects by two different columns in each select
[英]Union on two selects with a different number of columns
我正在嘗試在 MySQL 的結果集中再添加一列 output。 我可以通過執行兩個單獨的查詢輕松獲得這些結果,但我希望它們位於同一個結果集中,以便我可以將它們一起導出。
無論如何,這就是我正在做的事情:
select buildid, cast(status as char), eventtime from buildstatuses
where statustype = 8)
union all (select schedulerid, "", "" from builds
where builds.buildid in (select buildid from buildstatuses where statustype = 8));
我只將初始 select 中列出的列作為 output:
構建狀態事件時間
我沒有得到我想要的另一列: schedulerid
有任何想法嗎?
謝謝
編輯:更新
select buildstatuses.buildid, cast(status as char), eventtime, "" as schedulerid
from buildstatuses
inner join builds
on buildstatuses.buildid = builds.buildid
where buildstatuses.statustype = 8;
這就是我現在所在的位置,即使使用別名,想要添加的列 id 仍然顯示為空白
select buildid, cast(status as char), eventtime, schedulerid from buildstatuses
inner join builds on buildstatuses.buildid = builds.buildid
where statustype = 8
UNION
只會 select 兩個表之間共有的列。
您正在尋找的是加入。
編輯:最初我誤解了這個問題並建議對union
進行更改。 我更新了答案以使用應該提供您正在尋找的內容的join
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.