php - mysql 类似带有逗号的值

标签 php mysql sql-like

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

+-----------+----------+-------------+
|   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/

相关文章:

php - mysql OR 查询与输出

php - 使用多个复选框名称查询搜索

php - 单击提交按钮时保留 <input type ="file"> 字段中的值

MySQL 5.7.9 root 用户无法在 Fedora 23 上运行

ColdFusion Query-of-Queries Wildcard LIKE 条件不适用于单引号?

mysql - 如何在mysql的一组数据中使用like函数?

java - Hibernate 查询之类的问题

php - 与 array_intersect 相反

php - 如何在下拉菜单中显示图像

sql - 不同的问题!