Mysql不等于运算符失败?

时间:2015-01-12 21:04:06

标签: mysql sql

我有以下mysql表(简化):

CREATE TABLE IF NOT EXISTS `mytable` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`emails` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`phones` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`history` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,

PRIMARY KEY (`id`),
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=259 ;

我有以下查询:

SELECT *
FROM mytable
WHERE emails LIKE '%addr@yahoo.com%'
OR phones LIKE '%addr@yahoo.com%'
AND history !='None'

这产生了2条记录:

INSERT INTO `mytable` (`id`, `emails`, `phones`, `history`) VALUES
(118, 'PLEASE SET', NULL, 0, 'addr@yahoo.com', NULL, 0, 'None'),
(237, 'PLEASE SET', NULL, 0, 'addr@yahoo.com', NULL, 0, 'gomez');

但是我期待不包含history = 'None'的那个。我做错了什么?

3 个答案:

答案 0 :(得分:2)

您应该在WHERE子句中明确设置括号:

WHERE (all_emails LIKE '%addr@yahoo.com%'
   OR all_phones LIKE '%addr@yahoo.com%')
  AND history !='None'

答案 1 :(得分:2)

OR

中整理您的()条件
SELECT *
FROM mytable
WHERE (all_emails LIKE '%addr@yahoo.com%'
OR all_phones LIKE '%addr@yahoo.com%')
AND history !='None

Sample Demo

答案 2 :(得分:0)

SELECT *
FROM mytable
WHERE (all_emails LIKE '%addr@yahoo.com%'
OR all_phones LIKE '%addr@yahoo.com%')
AND history NOT IN(
SELECT history FROM mytable WHERE history LIKE 'None')