![](/img/trans.png)
[英]Get all records where each words in string exists on any of the columns in a table
[英]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')
对于上面的数据集,我正在尝试编写一个 SQL 查询,该查询返回行id
的1
、 2
和3
。 这三个id
在 columnA 中的值在 columnB 的任何行中都以 substring 的形式存在。
1
行( john smith
)中的john
在第2
行( columnA
)中作为 substring columnB
2
行( columnB
)中的orange
在第5
行( columnA
)中作为orange
存在3
行 ( columnB
) 中的alex
在第3
行 ( columnA
) 中作为alex
存在您可以在列名称上连接通配符,以便 SQL 在更大的字符串中搜索该字符串。
SELECT * FROM names WHERE '%'+ columnA + '%' in (select '%' + columnB + '%' from cte)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.