DELETE using LEFT JOIN with LIMIT in MySQL

后端 未结 1 999
无人共我
无人共我 2021-01-11 18:55

I\'m trying to delete orphan posts in my database and I have created this query:

DELETE post.*
      FROM foro_post AS post
      LEFT JOIN foro_thread AS th         


        
相关标签:
1条回答
  • 2021-01-11 19:32

    You can't use LIMIT directly within DELETE when you're referencing multiple tables at the same time, but you can get around that by encasing what you want to delete within a subselect:

    DELETE po 
    FROM   foro_post po
    JOIN   (
           SELECT    p.postid
           FROM      foro_post p
           LEFT JOIN foro_thread t ON p.threadid = t.threadid
           WHERE     t.threadid IS NULL
           ORDER BY  p.postid
           LIMIT     50
           ) pp ON po.postid = pp.postid
    
    0 讨论(0)
提交回复
热议问题