开发者

Like and Where in the same MySQL query

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜