基本上,表格的设置是这样的:
+-----------+----------+-------------+
| 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
来至少匹配这些逗号分隔值中存在的一项。
最佳答案
使用REGEXP
代替LIKE
(更多信息请参见: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$");
关于php - mysql 类似带有逗号的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11669153/