根据另一个表中的值删除一个表中的值

时间:2014-04-14 21:06:44

标签: mysql sql

我试图执行此语句来删除遵循列出的规则的F30026表中的记录。我可以使用该语句运行select * from和select count(*),但是在运行它时删除它不喜欢它...它会在'a'上丢失,即将F30026定义为表a

delete from CRPDTA.F30026 a
where exists (
select b.IMLITM from CRPDTA.F4101 b
where a.IELITM=b.IMLITM
and substring(b.IMGLPT,1,2) not in ('FG','IN','RM'));

谢谢!

3 个答案:

答案 0 :(得分:0)

这看起来像是我的内部联接,请参阅MySQL - DELETE Syntax

delete a from CRPDTA.F30026 as a
inner join CRPDTA.F4101 as b on a.IELITM = b.IMLITM
where substring(b.IMGLPT, 1, 2) not in ('FG', 'IN', 'RM')

请注意别名语法as aas b

答案 1 :(得分:0)

您可以匹配id(与在where子句中一样),而不是'exists'函数:

delete from CRPDTA.F30026 a
where a.IELITM IN (
select b.IMLITM from CRPDTA.F4101 b
where a.IELITM=b.IMLITM
and substring(b.IMGLPT,1,2) not in ('FG','IN','RM'));

答案 2 :(得分:0)

我相信这是你真正想要的,所有符合你标准的IELITM。

delete from CRPDTA.F30026
where IELITM IN (
select IMLITM from CRPDTA.F4101 
where substring(IMGLPT,1,2) not in ('FG','IN','RM'));