日期:2014-05-20  浏览次数:20794 次

救助。。。Mysql 的条件删除语句怎么写,就是删除部门的时候,如果部门下有用户,就不能删除
就是删除部门的时候,如果部门下有用户,就不能删除,没有就删除成功,救助。

------解决方案--------------------
不能用两条sql么

先查询部门下有没有员工

有的话再删除
------解决方案--------------------
这个意思?
SQL code
delete from 部门 where 部门ID='' and 部门ID not in ( select 部门ID from 人员)

------解决方案--------------------
探讨
不能用两条sql么

先查询部门下有没有员工

有的话再删除

------解决方案--------------------
use not exists to have try
SQL code
delete 部门 a where a.id = your_id
 and not exists (select 1 from 用户 where 用户.部门id = a.id)

------解决方案--------------------
默认外键有关联数据就不能删除.删除时会报错.

------解决方案--------------------
探讨