簡體   English   中英

返回 columnA 中的完整字符串在 columnB 的任何行中以 substring 形式存在的行

[英]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 查詢,該查詢返回行id123 這三個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)

嘗試以下幾個選項

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 || '%')

或/和

select * 
from your_table
where true
qualify string_agg(columnB, '|||') over() like '%' || columnA || '%'       

如果應用於您問題中的示例數據 - output(對於上述兩個選項)是

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM