【发布时间】:2019-03-19 10:07:45
【问题描述】:
我有两个表,其中一个列包含逗号分隔的数据。我需要比较这两列。逗号分隔的字段可以按任何顺序排列,但顺序无关紧要。
create table x_a (id1 integer, shared text);
create table x_b (id1 integer, shared text);
insert into x_a values
(1, 'A,B,C,D,E')
, (2, 'A,B,C,D,E');
insert into x_b values
(1, 'B,A,C,E,D')
, (2, 'B,A,C,E');
我使用了下面的查询,但它没有返回任何输出:
select a.id1,b.id1, a.shared, b.shared
from x_a a ,x_b b
where a.id1 = b.id1
and regexp_split_to_array(LOWER(a.shared),',')
= regexp_split_to_array(LOWER(b.shared),',')
我不能使用运算符&&,因为它会返回id=2,这是错误的,因为“共享”列不是精确的副本。
【问题讨论】:
-
因为
regexp_split_to_array(LOWER(a.shared),',') != regexp_split_to_array(LOWER(b.shared),','),因为abcde!=baced和abcde!=bace分别。 -
更改您的数据库/表设计。存储逗号分隔值违反了关系数据库的基本规则。
标签: sql arrays postgresql