简体   繁体   English

从另一个表的另一列中选择带有关键字的列

[英]Selecting a column with keywords from another column of another table

I need help with a probably simple SQL problem.我需要帮助解决一个可能很简单的 SQL 问题。 quite new with SQL so not sure how to solve this. SQL 很新,所以不知道如何解决这个问题。 My problem is that i'm trying to select a columnA where the data in the column contains keywords from another columnB in tableB.我的问题是我试图选择一个 columnA,其中该列中的数据包含来自 tableB 中另一列 B 的关键字。 an example of how the data is as below.数据如何的示例如下。

tableA, columnA : {'This apple is red', 'This ball is round', 'This chair is metal'}
tableB, columnB : {'red', 'round'}

Now im thinking something like this query现在我在想这样的查询

SELECT columnA FROM tableA
WHERE columnA LIKE '%' + (SELECT columnB FROM tableB) + '%'

So obviously it won't work because the subquery returns more than 1 value.所以显然它不会工作,因为子查询返回超过 1 个值。 I'm trying to see some other functions to use like CURSOR but I can't figure it out.我正在尝试查看其他一些功能,例如 CURSOR,但我无法弄清楚。 Help is very appreciated, thank you.非常感谢帮助,谢谢。

Use EXISTS instead:使用EXISTS代替:

SELECT columnA FROM tableA A
WHERE EXISTS (SELECT 1 FROM tableB B where A.columnA LIKE '%' + B.columnB + '%') 

You can try this:你可以试试这个:

select * from tableA A
JOIN tableB B on A.columnA  like '%' + B.columnB +'%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM