0

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
deHaar
  • 17,687
  • 10
  • 38
  • 51
Max
  • 538
  • 1
  • 6
  • 16
  • What exactly is the problem you have? It might be better to describe what you want to the underlying problem is you want to solve, rather then throwing some random SQL statement at us. **[EDIT]** your question and add some sample data and the expected output based on that data. [**Formatted text**](http://stackoverflow.com/help/formatting) please, [**no screen shots**](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557). **[edit]** your question - do **not** post code or additional information in comments. –  Jun 09 '17 at 12:50
  • can we see your attempt at the insert so far please? – jimmy8ball Jun 09 '17 at 13:06

1 Answers1

0

you can use alias() like in Usage of "aliased" in SQLAlchemy ORM for aliasing the 2nd time you use the table and the WHERE NOT EXITS has sqlalchemy logic eg in Using NOT EXISTS clause in sqlalchemy ORM query

ehacinom
  • 8,070
  • 7
  • 43
  • 65