为什么它与~~any()的工作方式不同? [英] Why it works different with ~~ any()?
本文介绍了为什么它与~~any()的工作方式不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我先给您看我的桌子:
INSERT INTO my_table(name, brand, source)
VALUES ('Abc', 'Abc', 'Orig'),
('Auchan', 'Auchan', 'Orig'),
('Auchan', 'Auchan', 'Added'),
('dj-auchan-djd', 'Auchan', 'Added'),
('Auchan', 'Other', 'Added'),
('Other', 'oj_auchan', 'Added'),
('Other', 'Other', 'Added');
我想要做的是删除来源是‘添加’的数据,品牌或名称类似于来源是‘ORIG’的品牌。例如,我们将在此处删除下一行:
('Auchan', 'Auchan', 'Added'), - simply name and brand is 'Auchan'
('dj-auchan-djd', 'Auchan', 'Added'), - brand ad name has 'Auchan'(but name lowercase)
('Auchan', 'Other', 'Added'), - name is 'Auchan'
('Other', 'oj_auchan', 'Added') - brand has 'Auchan' but lowercase.
那么,当我们手动创建此品牌比较时会是什么情况:
delete
from my_table
where lower(name) ~~ any
('{%auchan%,%abc%}') IS TRUE
or lower(brand) ~~ any
('{%auchan%,%abc%}') IS TRUE
and source = 'Added';
它运行得很好,我们用这个‘Auchan’删除了所有行。 但当我尝试汇总这些品牌的阵列时:
delete
from my_table
where lower(name) ~~ any
(select '{'||array_to_string(ARRAY_AGG(DISTINCT '%' || lower(brand) || '%'), ',')||'}'
from my_table
where source = 'Orig') IS TRUE
or lower(brand) ~~ any
(select '{' || array_to_string(ARRAY_AGG(DISTINCT '%' || lower(brand) || '%'), ',') || '}'
from my_table
where source = 'Orig') IS TRUE
and source = 'Added';
不会删除任何数据。
我甚至检查了它们是否相似,是的,这两个数组将相似...
有没有人可以帮忙(或者提供如何删除此事件的建议)?
推荐答案
问题是,子查询的结果被解释为字符串,而不是数组。这是因为这两种形式在语义上不同:
~~ ANY ('...') -- will be interpreted as an array literal
和
~~ ANY (SELECT ...) -- will compare with all query results in turn
所以您可以简单地写:
WHERE lower(name) ~~ ANY
(SELECT DISTINCT '%' || lower(brand) || '%'
FROM my_table
WHERE source = 'Orig')
这篇关于为什么它与~~any()的工作方式不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文