简体   繁体   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')

For the dataset above, I'm trying to write a SQL query that returns rows id 's 1 , 2 , and 3 .对于上面的数据集,我正在尝试编写一个 SQL 查询,该查询返回行id123 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.

相关问题 获取所有记录,其中字符串中的每个单词都存在于表中的任何列上 - Get all records where each words in string exists on any of the columns in a table 删除 ID 在 Redshift 中多次存在的行 - Delete rows where ID exists more than once in Redshift 当包含 substring 的小列表时,从字符串列返回所有 substring 值 - Return all substring values from a string column when contains a small list of substring 如何在 BigQuery 中取消连接字符串,删除另一列中存在的初始 substring? - How can I de-concatenate a string in BigQuery, removing the initial substring that exists in another column? 无法将类型“[String: Any]”的返回表达式转换为返回类型“Song?” - Cannot convert return expression of type '[String : Any]' to return type 'Song?' 使用 Count 语句和 Left Join 与 Where 子句返回所有行 - Return all rows using Count statement and Left Join with a Where Clause PartiQL 返回与单个行 userId 匹配的所有行 - PartiQL return all rows that match a single row userId 通过在 golang 中删除 substring 将字符串分成两部分 - Splitting string in 2 parts by removing substring in golang 存在 object 键的 Firestore 查询文档 - Firestore query document where object key exists getChildren 在特定键下计数 Firebase,其中存在特定值 - getChildren count in Firebase under a specific key, where a particular value exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM