在我的系统中有两个名为'faculty'和'faculty_assets'的表。
我需要从'教师'表中删除教员。但是当只有faculty_assets表为空时,系统应该允许删除教师。否则它不应该允许从'faculty'表中删除这些院系。
任何人都可以修改下面的SQL查询吗? ('fac_id'是'教师'表的主键)
function removeFaculty($fac_id){
$conn=new connection();
$sql="delete from faculty where fac_id='$fac_id'";
$result=$conn->query($sql);
return $result;
}
答案 0 :(得分:2)
您可以使用not exists
子句执行此操作:
delete from faculty
where fac_id = '$fac_id' and
not exists (select 1 from faculty_assets);
答案 1 :(得分:0)
尝试这样做:
DELETE t1 FROM faculty t1 LEFT JOIN faculty_assets t2 ON t1.fac_id=t2.fac_id WHERE t2.fac_id IS NULL;