[英]Subquery has too many columns
I have two tables with same structure: tmp_grn
and grn
. 我有两个结构相同的表:
tmp_grn
和grn
。
I have to delete rows from table tmp_grn
which already exists in table grn
. 我不得不删除表中的行
tmp_grn
已存在于表grn
。
Problem is I don't have a unique or primary key but I can determine a unique row with the combination of two columns. 问题是我没有唯一键或主键,但是我可以通过两列的组合来确定唯一行。 Let's say column names are
grn_code
and item_skucode
. 假设列名是
grn_code
和item_skucode
。
My query: 我的查询:
DELETE FROM tmp_grn
WHERE grn_code AND item_skucode IN
(SELECT grn_code , item_skucode FROM grn);
I am getting this error: 我收到此错误:
ERROR: subquery has too many columns
错误:子查询的列过多
What should be the right way to do this? 正确的方法应该是什么?
If you want to combine two columns, you need to put them into parenthesis: 如果要合并两列,则需要将它们放在括号中:
DELETE FROM tmp_grn
WHERE (grn_code, item_skucode) IN (SELECT grn_code, item_skucode
FROM grn);
But suslov's answer using an exists
is most probably faster - you need to check the execution plan to verify that. 但是suslov使用
exists
的答案很可能更快-您需要检查执行计划以确认这一点。
You can use exists
( if you want to check the pair of values ): 您可以使用
exists
( 如果要检查值对 ):
delete from tmp_grn t
where exists ( select *
from grn
where grn_code = t.grn_code
and item_skucode = t.item_skucode);
delete * from tmp_grn intersect select * from grn
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.