简体   繁体   English

SQL 加入另一个字段中包含的属性值

[英]SQL join on attribute value contained in another field

I would like to perform a join on attr_1 and attr_2 when:我想在 attr_1 和 attr_2 上执行连接:

  • attr_1 = attr_2 attr_1 = attr_2
  • attr_1 is contained in attr_2 attr_1 包含在 attr_2 中

A good match is performed if attr_1 is one of the value of attr_2 separated by '/'.如果 attr_1 是由“/”分隔的 attr_2 的值之一,则执行良好的匹配。

attr_1 attr_1 attr_2 attr_2
SOR:562索:562 ACCU:5698A/SOR:22/SOR:562累加器:5698A/SOR:22/SOR:562
SOR:120索:120 SOR:120/SOR:125 SOR:120/SOR:125
SOR:89索:89 SOR:1001/ACCU:569/SOR:56239/SOR:89 SOR:1001/ACCU:569/SOR:56239/SOR:89

But I don't want those matches as SOR:89 and SOR:899912 because it's not the same value (89 != 899912)但我不希望这些匹配为 SOR:89 和 SOR:899912 因为它的值不同 (89 != 899912)

attr_1 attr_1 attr_2 attr_2
SOR:89索:89 SOR:899912索:899912

How could I avoid wrong matches?我怎样才能避免错误的匹配? (I tried this but it doesnt work: (我试过这个但它不起作用:

on t.attr_1 LIKE CONCAT('%',s.attr_2,'%') ) on t.attr_1 LIKE CONCAT('%',s.attr_2,'%') ) 上

Instead of代替

on t.attr_1 LIKE CONCAT('%',s.attr_2,'%')

I think what you are trying to get is我认为你想要得到的是

on t.attr_2 LIKE CONCAT('%',s.attr_1,'%')

Convert the second attribute to an array:将第二个属性转换为数组:

select *
from first_table t1
  join second_table t2 on t1.attr1 = any(string_to_array(t2.attr2, '/'))

The condition should be:条件应该是:

ON CONCAT('/', attr_2, '/') LIKE CONCAT('%/', attr_1, '/%')

See a simplified demo .查看简化的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM