問題描述
union 連接的兩個sql 都包含 order
SELECT <property1>, <property2> FROM <table1> ORDER BY <condition> LIMIT 1
UNION ALL
SELECT <property1>, <property2> FROM <table2> WHERE <condition> ORDER BY <condition> LIMIT 1;
錯誤
ERROR: syntax error at or near "union" LINE 2: union all ^ ********** 錯誤 ********** ERROR: syntax error at or near "union" SQL 狀態: 42601 字符:149
解決方法
我找了很多資料,試了很多方法,都不行,最后是這樣解決的
(SELECT <property1>, <property2> FROM <table1> ORDER BY <condition>) UNION ALL (SELECT <property1>, <property2> FROM <table2> WHERE <condition> ORDER BY <condition>);
注意必須是 union all,不能是 union。
參考資料:
https://stackoverflow.com/questions/37352296/sql-union-all-with-order-by-and-limit-postgresql