How to query NOT EXISTS on the same table?

时间:2017-06-09 12:48:52

标签: postgresql-9.1 postgresql-9.4

How to query NOT EXISTS on the same table? I need to create the select to be able to perform the insert with the results. The idea is to only insert those records that do not exist in the table. The keys are t1.tie_id = T2.tie_id AND t1.org_id = t2.org_id. Currently in T1 I have a single record, which should put the remaining records.

SELECT
    101 tie_id,
    org_id,
    tie_org_orden,
    tie_org_activo,
    tie_org_default
FROM
    table1 T1 
WHERE
    NOT EXISTS (
        SELECT
            1
        FROM
            tabla2 T2
        WHERE
            t1.tie_id = T2.tie_id
            AND 
            t1.org_id = t2.org_id
    )
    AND
    t1.tie_id = 42 and t1.org_id = 181

0 个答案:

没有答案