簡體   English   中英

如何在 MariaDB 中使用 SQL 聯合的結果? (添加計算列,連接另一個表等)

[英]How to use result of SQL union in MariaDB? (add calculation column, join with another table, etc.)

我在使用聯合語法在 mariaDB 10.4 中查詢時遇到問題。

我運行這個查詢,沒問題,沒有錯誤:

-- select * from
((select HireDate HireTermDate from employee)
union
(select TerminationDate HireTermDate from employee
where TerminationDate is not null)
order by HireTermDate asc)

但是,如果我刪除對“select * from...”的評論,如下所示:

select * from
((select HireDate HireTermDate from employee)
union
(select TerminationDate HireTermDate from employee
where TerminationDate is not null)
order by HireTermDate asc)

它有錯誤,這是為什么?

提前謝謝了

當您刪除注釋時,帶有 union 的查詢變成了派生表。

這需要一個別名。

 create table test (id int) insert into test values (1), (2)
 select * from ( select id as num from test where id=1 union select id from test where id=2 order by num )
\n你的 SQL 語法有錯誤; 檢查與您的 MariaDB 服務器版本相對應的手冊,以了解在第 7 行的 '' 附近使用的正確語法\n
select * from ( select id as num from test where id=1 union select id from test where id=2 ) AS q order by num
\n | 數量 |\n |  --: |\n |  1 |\n |  2 |\n

db<> 在這里擺弄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM