[英]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.