我正在尝试从我的表is_active = 1
获取一些记录,但我的查询返回结果甚至记录is_active = 0
。我确信这是我的查询错误。
这是我的查询
SELECT `id`, `postal_code`, is_archived, is_active, `email`, `company`, `partner`, `telephone`, `description`
FROM `firm`
WHERE ('is_archived' =0 AND 'is_active' = 1)
AND `email` LIKE '%%' ESCAPE '!'
OR `company` LIKE '%%' ESCAPE '!'
OR `partner` LIKE '%%' ESCAPE '!'
OR `telephone` LIKE '%%' ESCAPE '!'
ORDER BY `id` DESC
LIMIT 10
请帮忙。
感谢。
答案 0 :(得分:0)
将OR
逻辑条件放在括号内,使优先级高于AND
:
SELECT
`id`, `postal_code`, is_archived, is_active, `email`, `company`, `partner`, `telephone`, `description`
FROM `firm`
WHERE
is_archived =0
AND is_active = 1
AND
(`email` LIKE '%%' ESCAPE '!'
OR `company` LIKE '%%' ESCAPE '!'
OR `partner` LIKE '%%' ESCAPE '!'
OR `telephone` LIKE '%%' ESCAPE '!')
ORDER BY `id` DESC
LIMIT 10
等待并且不要将列名放在单引号中:
'is_archived'
和'is_active'
答案 1 :(得分:0)
SELECT `id`, `postal_code`, is_archived, is_active, `email`, `company`,`partner`, `telephone`, `description`
FROM `firm` WHERE `is_archived` =0 AND `is_active` = 1 AND
(`email` LIKE '%%' ESCAPE '!'
OR `company` LIKE '%%' ESCAPE '!'
OR `partner` LIKE '%%' ESCAPE '!'
OR `telephone` LIKE '%%' ESCAPE '!')
ORDER BY `id` DESC LIMIT 10;
不要在列中添加列名 单引号
答案 2 :(得分:-1)
你可以试试这个:
SELECT `id`, `postal_code`, is_archived, is_active, `email`, `company`, `partner`, `telephone`, `description`
FROM `firm`
WHERE 'is_archived' = 0 AND 'is_active' = 1
AND (`email` LIKE '%%' ESCAPE '!'
OR `company` LIKE '%%' ESCAPE '!'
OR `partner` LIKE '%%' ESCAPE '!'
OR `telephone` LIKE '%%' ESCAPE '!')
ORDER BY `id` DESC
答案 3 :(得分:-1)
你可能意味着这个:
SELECT `id`, `postal_code`, is_archived, is_active, `email`, `company`,
`partner`, `telephone`, `description`
FROM `firm`
WHERE (`is_archived` =0 AND `is_active` = 1)
AND (`email` LIKE '%%' ESCAPE '!'
OR `company` LIKE '%%' ESCAPE '!'
OR `partner` LIKE '%%' ESCAPE '!'
OR `telephone` LIKE '%%' ESCAPE '!')
ORDER BY `id` DESC
LIMIT 10
你遗漏了括号。请参阅以下关于operator precedence的文章 你错误地引用了列is_archived和is_active,正如Nirav正确指出的那样。
答案 4 :(得分:-1)
这是因为您在此处将string
与numeric
值进行比较,
WHERE ('is_archived' =0 AND 'is_active' = 1)
应该是,
WHERE (`is_archived` = 0 AND `is_active` = 1)
不应使用'
引用列名称
所以完整的查询应该是,
SELECT `id`, `postal_code`, is_archived, is_active, `email`, `company`, `partner`, `telephone`, `description`
FROM `firm`
WHERE (`is_archived` =0 AND `is_active` = 1)
AND (`email` LIKE '%%' ESCAPE '!'
OR `company` LIKE '%%' ESCAPE '!'
OR `partner` LIKE '%%' ESCAPE '!'
OR `telephone` LIKE '%%' ESCAPE '!')
ORDER BY `id` DESC
LIMIT 10