[英]How to query a database for empty tables
In my system there are two tables called 'faculty' and 'faculty_assets'. 在我的系统中,有两个表,分别称为“ faculty”和“ faculty_assets”。
I need to delete faculty by faculty from the 'faculty' table. 我需要从“教职员工”表中删除各教职员工。 But system should allow deleting the faculty when only faculty_assets table empty.
但是当只有faculty_assets表为空时,系统应该允许删除该教师。 Otherwise It should not allows to delete the faculties from 'faculty' table.
否则,它不应允许从“教职员工”表中删除教职员工。
Can anyone modify below sql query? 任何人都可以在下面的SQL查询中进行修改吗? ('fac_id' is the primary key of 'faculty' table )
(“ fac_id”是“教职”表的主键)
function removeFaculty($fac_id){
$conn=new connection();
$sql="delete from faculty where fac_id='$fac_id'";
$result=$conn->query($sql);
return $result;
}
You can do this with a not exists
clause: 您可以使用
not exists
子句来做到这一点:
delete from faculty
where fac_id = '$fac_id' and
not exists (select 1 from faculty_assets);
尝试这样做:
DELETE t1 FROM faculty t1 LEFT JOIN faculty_assets t2 ON t1.fac_id=t2.fac_id WHERE t2.fac_id IS NULL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.