DELETE + JOIN + ORDER BY + LIMIT = syntax error

前端 未结 3 1635
[愿得一人]
[愿得一人] 2021-01-13 23:49

Drop the ORDER BY + LIMIT, or the JOIN, and everything is peaches. Put them together and I seem to release the Kraken

3条回答
  •  花落未央
    2021-01-14 00:12

    From Mysql Docs: DELETE

    For the multiple-table syntax, DELETE deletes from each tbl_name the rows that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.

    In your case, I think this works:

    DELETE 
    FROM table1
    WHERE EXISTS
          ( SELECT t2.id
            FROM table2 AS t2
            WHERE t2.id = table1.id
              AND t2.field = 'something'
          ) 
    ORDER BY id DESC
    LIMIT 5
    

提交回复
热议问题