MySQL 如何将 LIKE 与列名一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1398720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:04:31  来源:igfitidea点击:

how to use LIKE with column name

sqlmysql

提问by ArK

Normally LIKEstatement is used to check the pattern like data.

通常LIKE语句用于检查模式之类的数据。

example:

例子:

select * from table1 where name like 'ar%'

My problem is to use one column of table with LIKEstatement.

我的问题是在LIKE语句中使用一列表。

example:

例子:

select * from table1, table2 where table1.x is like table2.y%

Query above results error . how to use one column data in likequery?

查询上述结果错误。如何在like查询中使用一列数据?

回答by MatBailie

You're close.

你很接近。

The LIKE operator works with strings (CHAR, NVARCHAR, etc). so you need to concattenate the '%' symbol to the string...

LIKE 运算符处理字符串(CHAR、NVARCHAR 等)。所以你需要将 '%' 符号连接到字符串...



MS SQL Server:

微软 SQL 服务器:

SELECT * FROM table1,table2 WHERE table1.x LIKE table2.y + '%'


Use of LIKE, however, is often slower than other operations. It's useful, powerful, flexible, but has performance considerations. I'll leave those for another topic though :)


但是,LIKE 的使用通常比其他操作慢。它有用、强大、灵活,但有性能方面的考虑。不过,我会将它们留给另一个主题:)



EDIT:

编辑:

I don't use MySQL, but this may work...

我不使用 MySQL,但这可能有效......

SELECT * FROM table1,table2 WHERE table1.x LIKE CONCAT(table2.y, '%')

回答by RoRo

SQL SERVER

SQL服务器

 WHERE ColumnName LIKE '%'+ColumnName+'%'

回答by Tomalak

...
WHERE table1.x LIKE table2.y + '%'

回答by Koekiebox

declare @LkeVal as Varchar(100)
declare @LkeSelect Varchar(100)

Set @LkeSelect = (select top 1 <column> from <table> where <column> = 'value')
Set @LkeVal = '%' + @LkeSelect

select * from <table2> where <column2> like(''+@LkeVal+'');

回答by alvaroIdu

ORACLE DATABASE example:

ORACLE 数据库示例:

select * 
from table1 t1, table2 t2
WHERE t1.a like ('%' || t2.b || '%')

回答by bhaskar

For SQLLite you will need to concat the strings

对于 SQLLite,您需要连接字符串

select * from list1 l, list2 ll 
WHERE l.name like "%"||ll.alias||"%";