读书人

救助。Mysql 的条件删除语句如何写就

发布时间: 2012-03-23 12:06:21 作者: rapoo

救助。。。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)
[解决办法]
默认外键有关联数据就不能删除.删除时会报错.

[解决办法]
探讨


读书人网 >Java相关

热点推荐