如何使用SQL检查一列中的字符串值是否部分包含在另一列的字符串值中? [英] How to check if string value in one column is partially contained in string value of another column using SQL?

查看:814
本文介绍了如何使用SQL检查一列中的字符串值是否部分包含在另一列的字符串值中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用SQL检查一列中的字符串值是否部分包含在另一列中?

如果我必须检查tbl表的col_1列是否包含"Mercury",我将执行以下查询:

SELECT * FROM tbl WHERE col_1 LIKE '%Mercury%'

但是如果我必须检查每一行,如果col_2的完整值部分包含在col_1中,那我该怎么做.

类似的东西:

SELECT * FROM tbl WHERE col_1 LIKE col_2 

不起作用. 我有一个涉及SUBSTRINSTRLENGTH的解决方案,但这使我的查询速度太慢. 请发表您的建议. 我想在MySQL中做到这一点.

解决方案

SELECT * FROM tbl WHERE col_1 LIKE '%'+col_2+'%'


对于MySQL,等效语法为:

SELECT * FROM tbl WHERE col_1 LIKE CONCAT('%',col_2,'%')

How do I check if string value in one column is partially contained in string value of another column using SQL?

If I had to check if col_1 column of tbl table contains 'Mercury', I would do a query like:

SELECT * FROM tbl WHERE col_1 LIKE '%Mercury%'

But if I had to check that for every row, if the full value of col_2 is contained partially in col_1, how would I do that.

Something like:

SELECT * FROM tbl WHERE col_1 LIKE col_2 

won't work. I have a solution which involves SUBSTR, INSTR and LENGTH but that makes my query too slow. Please post your suggestions. I want to do this in MySQL.

解决方案

SELECT * FROM tbl WHERE col_1 LIKE '%'+col_2+'%'


EDIT: For MySQL, the equivalent syntax would be:

SELECT * FROM tbl WHERE col_1 LIKE CONCAT('%',col_2,'%')

这篇关于如何使用SQL检查一列中的字符串值是否部分包含在另一列的字符串值中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆