Mysql - how to use like on multiple columns

久未见 提交于 2019-12-23 02:42:23

问题


I've searched in mysql query a word in multiple column in java program. The number of column is variable. It is correct this query:

select * from customer with (city, name) like%'adelaide'%

回答1:


You can use CONCAT() function:

select * from customer WHERE concat(city,name) like '%adelaide%'

You can add as many columns to the concat function as you like. Also as you see I changed your like syntax, '%WORD%' and used a simple where clause.




回答2:


It's better to use CONCAT_WS() function.

CONCAT() returns NULL if any argument is NULL.

CONCAT_WS() skip any NULL values after the separator argument.




回答3:


multiple like statements can not be used with or directly. You have to use column name for each like statement.

Use multiple like as mentioned below.

Select * 
from animals 
where 
(
[animalscolumn] like ('%dog%') or 
[animalscolumn] like ('%cat%') or 
[animalscolumn] like ('%gerbil%') or 
[animalscolumn] like ('%hamster%') or 
[animalscolumn] like ('%guinea pig%')
)



回答4:


select * from customer with city like%'adelaide'% or name like%'adelaide'%


来源:https://stackoverflow.com/questions/35915296/mysql-how-to-use-like-on-multiple-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!