从两个表中删除数据

时间:2015-04-28 08:08:09

标签: mysql mysqli delete-row

我希望从以下两个表中删除数据:员工 staff_take_courses 。 SQLCommand行不断给出错误:

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`Exclusif`.`staff_take_courses`, CONSTRAINT `fk_staff_has_courses_staff1` FOREIGN KEY (`staff_id`) REFERENCES `staff` (`id`) ON DELETE NO ACTION O
N UPDATE NO ACTION)

这是查询:

  delete s, stc from staff s inner join staff_take_courses stc on stc.staff_id = s.id where s.id= '$id';

请帮帮我

3 个答案:

答案 0 :(得分:2)

为什么你只能删除两个声明:

 DELETE FROM staff_take_courses WHERE staff_take_courses.staff_id= '$id';
 DELETE FROM staff WHERE staff.id= '$id';

表格之间的关系一定存在问题。这不允许你从表中删除

答案 1 :(得分:1)

从staff_take_courses中删除staff_take_courses.staff_id =' $ id&#39 ;;     从员工中删除WHERE staff.id =' $ id&#39 ;;     OR

Just use INNER JOIN as below

DELETE field1 , field2  FROM staff_take_courses  INNER JOIN staff  
WHERE staff_take_courses.staff_id= staff.id and staff_take_courses.staff_id = '1'

答案 2 :(得分:0)

如果该表与另一个表有关系,那么您无法从数据库中删除该表。为什么不尝试检查你们的关系呢?