基本上,表的设置是这样的:

+-----------+----------+-------------+
|   land    |   city   |  perimeter  |
+-----------+----------+-------------+
|  America  |  Kansas  |      1      |
|  Britain  |  Berlin  |      4      |
|  Japan    |  Tokyo   |      5      |
+-----------+----------+-------------+


我当前的查询:

$query = "SELECT land, city, perimeter FROM agents WHERE land LIKE ? OR city LIKE ? OR perimeter LIKE ?";
$params = array("%China%","%Kansas%","%6%");


此查询有效,它将返回America,Kansas,1。但是,如果我的params等于:

$params = array("%China%","%Beijing,London,Kansas,Bali%","%6%");


这将不会返回任何内容。如果在逗号分隔的值中存在一项,则如何在逗号分隔的值中使用LIKE进行匹配。

最佳答案

代替LIKE使用REGEXP(更多信息在这里:http://dev.mysql.com/doc/refman/5.0/en/regexp.html)。

$query = "SELECT land, city, perimeter FROM agents
   WHERE land REGEXP ? OR city REGEXP ? OR perimeter REGEXP ?";
$params = array("^China$","^Beijing|London|Kansas|Bali$","^6$");

09-25 17:15