开发者

Like and Where in the same MySQL query

开发者 https://www.devze.com 2022-12-22 02:50 出处:网络
I would like to use something like 开发者_JAVA百科the following: SELECT city FROM cities WHERE city LIKE %D% AND country_id = \'12\'

I would like to use something like 开发者_JAVA百科the following:

SELECT city FROM cities WHERE city LIKE %D% AND country_id = '12'


You need to quote the string

SELECT city FROM cities WHERE city LIKE '%D%' AND country_id = '12'

But remember that using a LIKE with a pattern starting with a "%" means the server will NOT use an index on 'city' column - it may not matter in your specific case but something to be aware of. Here's the reference since your comment indicates you're not familiar with indexes.


Bit late to the show... But I had the same problem when using it as a "search" feature. Remember in PHP, you need to concatenate it or it might break... Like this

$query="SELECT city FROM cities WHERE city LIKE '%".$searchterm."%' AND country_id = '12'"

Maybe not relevant to you but if like me you looked around for ages to find out why the "search term" was not being put in in a recognisable format, this might help.

0

精彩评论

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