带 order by 和 limit 的 SQL Union All (Postgresql)

2024-02-13

在以下查询中我收到语法错误:

SELECT <property1>, <property2>
FROM <table1> 
ORDER BY <condition> LIMIT 1
UNION  ALL
SELECT <property1>, <property2>
FROM <table2> 
WHERE <condition> ORDER BY <condition> LIMIT 1;

“UNION”处或附近的语法错误 第 4 行:UNION ALL

每一个SELECT独立执行良好。我的猜测是关于ORDER BY... LIMIT 1 maybe?


将每个查询包装起来():

(SELECT <property1>, <property2>
FROM <table1> 
ORDER BY <condition> LIMIT 1)
UNION  ALL
(SELECT <property1>, <property2>
FROM <table2> 
WHERE <condition> ORDER BY <condition> LIMIT 1);

SqlFiddleDemo http://sqlfiddle.com/#!9/9eecb7d/60265/0

您还可以订购最终查询:

(SELECT 'a' AS col
ORDER BY col LIMIT 1)
UNION ALL 
(SELECT 'b' AS col
ORDER BY col  LIMIT 1)
ORDER BY  col DESC
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带 order by 和 limit 的 SQL Union All (Postgresql) 的相关文章

随机推荐