一个关于sql查询命令的问题,请高手帮忙

来源:百度知道 编辑:UC知道 时间:2024/06/05 05:19:54
如果用查询语句查询下列要求
比如一个表中
charid name ...
1001 asdf
1002 asdf
在另一个表中
id name charid
1 asd 1001
2 dsd 1002
现在我用delete命令把表一中的一些数据删了 但是因为 我表二中的一个字段是表1的ID 怎么样才能把 表2中 charid在表一中不存在的记录删除?

delete from 表2 where charid not in (select charid from 表1)
或:
delete from 表2 where not exists (select 1 from 表1 where charid=表2.charid)

推荐使用第二种方法,因为效率要比第一种高的多

不是有级联关键字吗 cascade,进行级联删除就行了

delete from 表1,表2 where 表1.charid=表2.charid and charid=?

用级联删除就行了 触发器