Using EXCEPT clause in PostgreSQL

前端 未结 1 1378
星月不相逢
星月不相逢 2020-12-21 02:02

I am trying to use the EXCEPT clause to retrieve data from table. I want to get all the rows from table1 except the one\'s that exist in tabl

相关标签:
1条回答
  • 2020-12-21 02:33

    Your query seems perfectly valid:

    SELECT fk_id_tbl2 AS some_name
    FROM   table1
    EXCEPT  -- you may want to use EXCEPT ALL
    SELECT pk_id
    FROM   table2;
    

    Column names are irrelevant to the query. Only data types must match. The output column name of your query is fk_id_tbl2, just because it's the column name in the first SELECT. You can use any alias.

    What's often overlooked: the subtle differences between EXCEPT (which folds duplicates) and EXCEPT ALL - which keeps all individual unmatched rows. More explanation and other ways to do the same, some of them much more flexible:

    • Select rows which are not present in other table

    Details for EXCEPT in the manual.

    0 讨论(0)
提交回复
热议问题