How to delete rows using an outer join

前端 未结 2 2013
無奈伤痛
無奈伤痛 2021-02-20 06:44

I\'ve got a problem to delete records from a PostgreSQL table, using a LEFT JOIN.

I\'d like to delete rows I get with the following query:

S         


        
相关标签:
2条回答
  • 2021-02-20 07:12

    Good work, sun. Minor suggestion: when using EXISTS/NOT EXISTS you don't need to SELECT *. A common convention (docs) is to just write SELECT 1 as like this:

    DELETE FROM url WHERE NOT EXISTS (
      SELECT 1 FROM link_type WHERE url.link_type = link_type.id 
    );
    

    Functionally, both ways work.

    0 讨论(0)
  • 2021-02-20 07:22

    Still don't understand why my previous query doesn't work (if someone could explain, would be nice), but here is how I did the trick:

    DELETE FROM url WHERE NOT EXISTS (SELECT * FROM link_type WHERE url.link_type = link_type.id );
    
    0 讨论(0)
提交回复
热议问题