MySQL加入删除

时间:2012-12-17 06:36:28

标签: php mysql join phpmyadmin left-join

我有多张桌子 list_of_students,sport_students。

在sport_students中我有一个名为

的字段

list_of_house_id和list_of_students我有字段list_of_class_id和list_of_house_id。

现在我需要删除sport_student acc中的记录,以及house_id和list_of_class_id。

我已尝试此查询但似乎无法正常工作

DELETE FROM `sport_students` JOIN list_of_students as student on `sport_student.list_house_id`= `student.list_of_house_id` WHERE `sport_student.list_of_house_id`=1 and `list_of_students.list_of_class_id`=1

有人知道怎么做吗?

2 个答案:

答案 0 :(得分:0)

试试这个:

DELETE sp
  FROM `sport_students` sp
  JOIN list_of_students as student 
    ON sp.list_house_id = student.list_of_house_id
 WHERE sp.list_of_house_id=1 
   AND student.list_of_class_id=1;

有关详情,请参阅MySQL: Delete Syntax

答案 1 :(得分:0)

DELETE  FROM sport_students
WHERE   list_of_house_id IN (
            SELECT  list_of_house_id
            FROM    list_of_students
            WHERE   list_of_class_id = 1 AND list_of_house_id =1 
        )