SQL:检查列是否包含来自另一个SELECT语句的值

时间:2018-07-14 21:18:06

标签: sql sql-server join ssms

我想加入以下两个表:

表1:

---------
| Brand |
---------
| Honda |
| Dodge |
| Toyota|
---------

表2调查表:

------------------------------------------
|Ser|          Comments                  |
------------------------------------------
| 1 | Honda is a reliable brand          |
| 2 | The dodge Challenger is pure muscle|
| 3 | The new Toyota Corolla is a treat  |

现在,我想同时加入这两个表并获取表3:

--------------------------------------------------
|Ser|          Comments                  | Brand |
--------------------------------------------------
| 1 | Honda is a reliable brand          | Honda |
| 2 | The dodge Challenger is pure muscle| Dodge |
| 3 | The new Toyota Corolla is a treat  | Toyota|

如何获得表3?

我无法同时比较评论和品牌列表,以查看评论是否包含品牌名称,如果是,则使用品牌名称。任何帮助都会很棒。

2 个答案:

答案 0 :(得分:2)

您可以在JOIN上尝试like

SELECT * 
FROM Table1 t1 INNER JOIN Table2 t2 
ON t2.Comments like '%'+t1.Brand  +'%'

Sqlfiddle

答案 1 :(得分:1)

使用LIKE: 其中UPPER(注释)喜欢'%'||品牌|| '%'