我有以下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'
的那个。我做错了什么?
答案 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
答案 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')