Postgresql
“AS”處或附近的語法錯誤
我有一個程序,它從儲存在引用表中的列表中獲取一個或多個表名,並在其上執行 SELECT 查詢。所以我檢索表名並將其插入到循環中的 SELECT 查詢中。
(SELECT COUNT(*) FROM (SELECT col1, col2, col3, col4 FROM table1 UNION SELECT col1, col2, col3, col4 FROM table2 UNION SELECT col1, col2, col3, col4 FROM table3 UNION SELECT col1, col2, col3, col4 FROM table4 ) t1 WHERE //where conditions AS name
這是錯誤
Query 1 ERROR: ERROR: syntax error at or near "AS" LINE 21: AS name
如果 AS 子句不存在,則查詢有效 ^
“AS”子句不能用於 WHERE 條件。
它用於為表/視圖、列和返回結果集的查詢提供別名,例如子查詢和相關查詢。
像
SELECT MyTable.Column_A AS MyColumn --MyColumn is alias for Column_A FROM Table_A AS MyTable --Like wise MyTable is alias for Table_A WHERE Column_B [Conditional Operator] [Input Value]
所以,你的查詢應該像
SELECT COUNT(*) AS [Count] FROM (SELECT col1, col2, col3, col4 FROM table1 UNION SELECT col1, col2, col3, col4 FROM table2 UNION SELECT col1, col2, col3, col4 FROM table3 UNION SELECT col1, col2, col3, col4 FROM table4 ) t1 WHERE t1.column [Conditional Operator] [Input Value]
謝謝!