使用 CTE 可以在多个查询中重用子查询的结果
SELECt。
为此,您需要PostgreSQL 8.4+:
WITH x AS (SELECT carto_id_key FROM table1 WHERe tag_id = 16)SELECt carto_id_keyFROM xUNIOn ALLSELECt t2.some_other_id_keyFROM xJOIN table2 t2 ON t2.carto_id_key = x.carto_id_key
您最有可能想要
UNIOn ALL而不是
UNIOn。不排除重复项,这样可以更快。



