开发者

How to use like function in a set of data in mysql?

is there a way i can use like function in a set of data? i tried select * from table1 where column1 like ('%1%','%2%','%3%','%4%'); but it didn't work.

my scenario is having 2 select statements such as select * from class1 where firstname in (select name from class2 where firstname = 'greg');

but instea开发者_开发知识库d of having class1.firstname = class2.firstname i wanted it to be class1.firstname like concat('%',class2.firstname,'%');


You could "OR" them:

... WHERE (column1 like '%1%') OR (column1 like '%2%') OR ...


like takes one argument

select * 
from table1 
where column1 like ('%1%'); 

if you want to check for many then use OR or UNION operators

select * 
from table1 
where column1 like ('%1%')
or column1 like ('%2%'); 


Normally you would use the IN function when you have a list of strings to compare to. But here you need a combination of LIKE and IN, and as far as I know there is no function in SQL that does this, so you'll need to use the OR operator.


You can use the execute to build the dynamic sql, but you must beware of safety and performance issues that may arise.

Another options is using regex

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜