MySQL delete row from multiple tables

后端 未结 3 1063
借酒劲吻你
借酒劲吻你 2020-12-01 17:46

Is this the correct way to do it?

DELETE t1, t2, t3, t4 FROM 
  table1 as t1 
  INNER JOIN  table2 as t2 on t1.id = t2.id
  INNER JOIN  table3 as t3 on t1.id         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 18:02

    An easy way to figure it out is to first write it as a query:

    SELECT * FROM 
            table1 as t1 
            INNER JOIN  table2 as t2 on t1.id = t2.id
            INNER JOIN  table3 as t3 on t1.id=t3.id
            INNER JOIN  table4 as t4 on t1.id=t4.id
            WHERE  t1.username='%s' AND t1.id='%s'
    

    If you get the results you expect, just replace the *Select ** with Delete and your table names. Then it would become:

    Delete t1, t2, t3, t4 From table1 as t1 
            INNER JOIN  table2 as t2 on t1.id = t2.id
            INNER JOIN  table3 as t3 on t1.id=t3.id
            INNER JOIN  table4 as t4 on t1.id=t4.id
            WHERE  t1.username='%s' AND t1.id='%s'
    

提交回复
热议问题