mysql - 将数字条件与 like 组合中断 MySQL 查询

标签 mysql

我有一个包含 VARCHAR(255) 列和一个 numeric(15, 2) 列的表。

如果我执行此搜索,它会起作用:

select title, cost, pageUrl
from products
where cost between 100 and 125
;

如果我执行此搜索查询,它会起作用:

select title, cost, pageUrl
from products
where title like '%something%'
 or pageUrl like '%something%'
;

但是当我组合它们时,它会忽略数字过滤器:

select title, cost, pageUrl
from products
where cost between 100 and 125
and title like '%something%'
 or pageUrl like '%something%';

我尝试了几种不同的组合查询变体,但我一直遇到同样的问题。任何指导将不胜感激。

最佳答案

你应该使用括号:

select title, cost, pageUrl
from products
where cost between 100 and 125
      and (title like '%something%' or pageUrl like '%something%');

否则,数字过滤器将与第一个 like 谓词组合,并单独评估第二个 like 谓词。

可以查看Operator Precedence如果您愿意,可以在手册中找到。

关于mysql - 将数字条件与 like 组合中断 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31146425/

相关文章:

MySQL:在不使用子查询的情况下选择数据集的最小值

mysql - 从表中选择取决于两列中的值

mysql - 如何将mysql_real_escape_string()的结果插入到oracle数据库中?

mysql - 如何从 mySQL 中的字符串中提取前 2 个字符?

mysql - CakePHP 3 : How to count and retrieve data for different periods in one query?

mysql - 如何按特定日期时间自动将数据插入mysql

php - 连接表上的 ORDER BY

MySQL数据库注入(inject)攻击后含有恶意SCRIPT标签?

MySQL:如果选择不返回任何行,则选择不同的内容

php - 删除用户帐户的按钮