Using the EXCEPT clause in PostgreSQL

I am trying to use a clause EXCEPTto retrieve data from a table. I want to get all rows from table1except those that exist in table2. As far as I understand, the following will not work:

CREATE TABLE table1(pk_id int, fk_id_tbl2 int);
CREATE TABLE table2(pk_id int);

Select fk_id_tbl2
FROM table1
Except
Select pk_id
FROM table2

The only way I can use EXCEPTis to select from the same tables or select columns with the same column name from different tables.

Can someone explain how to use the explanation clause better?

+4
source share
1 answer

Your query seems perfectly true:

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

Column names . . fk_id_tbl2, , SELECT. .

: EXCEPT ( ) EXCEPT ALL - . , :

EXCEPT .

+4

Source: https://habr.com/ru/post/1628227/


All Articles