[英]Return rows where the full string in columnA exists as substring in ANY row of columnB
INSERT INTO names
(`id`,`columnA`,`columnB`)
VALUES
(1,'john','dog'),
(2,'orange','john smith'),
(3,'alex','alex'),
(4,'match','man'),
(5,'pony','orange')
For the dataset above, I'm trying to write a SQL query that returns rows id
's 1
, 2
, and 3
.对于上面的数据集,我正在尝试编写一个 SQL 查询,该查询返回行
id
的1
、 2
和3
。 These three id
's have values in columnA that exist as a substring in ANY row of columnB.这三个
id
在 columnA 中的值在 columnB 的任何行中都以 substring 的形式存在。
john
in row 1
( columnA
) exists as a substring in john smith
row 2
( columnB
)1
行( john smith
)中的john
在第2
行( columnA
)中作为 substring columnB
orange
in row 2
( columnA
) exists as a substring in orange
row 5
( columnB
)2
行( columnB
)中的orange
在第5
行( columnA
)中作为orange
存在alex
in row 3
( columnA
) exists as a substring in alex
row 3
( columnB
)3
行 ( columnB
) 中的alex
在第3
行 ( columnA
) 中作为alex
存在You can concatenate a wildcard on a column name so that SQL searches for the string within a larger string.您可以在列名称上连接通配符,以便 SQL 在更大的字符串中搜索该字符串。
SELECT * FROM names WHERE '%'+ columnA + '%' in (select '%' + columnB + '%' from cte) SELECT * FROM names WHERE '%'+ columnA + '%' in (select '%' + columnB + '%' from cte)
Try below few options尝试以下几个选项
select any_value(a).*
from your_table a, your_table b
group by to_json_string(a)
having logical_or(b.columnB like '%' || a.columnA || '%')
or/and或/和
select *
from your_table
where true
qualify string_agg(columnB, '|||') over() like '%' || columnA || '%'
If applied to sample data in your question - output (for both above options) is如果应用于您问题中的示例数据 - output(对于上述两个选项)是
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.