我有這個標記表開關與LIKE SELECT查詢裏面的MySQL
CREATE TABLE IF NOT EXISTS `Tags` (
`id_tag` int(10) unsigned NOT NULL auto_increment,
`tag` varchar(255) default NULL,
PRIMARY KEY (`id_tag`),
UNIQUE KEY `tag` (`tag`),
KEY `id_tag` (`id_tag`),
KEY `tag_2` (`tag`),
KEY `tag_3` (`tag`),
KEY `tag_4` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2937 ;
INSERT INTO `Tags` (`id_tag`, `tag`) VALUES
(1816, '(class'),
(2642, 'class\r\n\r\nâ?¬35'),
(1906, 'class\r\nif'),
(1398, 'class'),
(2436, 'class)'),
(1973, 'class:\n1.'),
(2791, 'classes'),
(1325, 'New'),
(2185, 'pack'),
(1905, 'packed'),
(1389, 'WebClass');
我想取其中標籤匹配關鍵字class
或pack
或new
,與指示另一場沿所有記錄,其中3個關鍵字實際上與標籤字段相匹配。
下面的查詢不給出正確的結果 查詢1
select id_tag,
case tag
when tag LIKE "%class%" then "class"
when tag LIKE "%new%" then "new"
when tag LIKE "%pack%" then "pack"
end as matching_tag
from Tags
where tag LIKE "%class%" OR tag LIKE "%new%" OR tag LIKE "%pack%"
我必須使用類似的情況中。否則完成匹配工作。下面的查詢工作: -
查詢2
select id_tag,
case tag
when "class" then "class"
when "new" then "new"
when "pack" then "pack"
end as matching_tag
from Tags
where tag = "class" OR tag = "new" OR tag = "pack"
有什麼不對查詢1.請幫助。
+1 DDL和樣本數據 – Unreason 2010-06-21 07:56:52
這裏有什麼問題嗎?查詢1給出了11個結果,並且有11個條目,這是預期的。查詢2中的where子句不同並更改結果集。 – Cez 2010-06-21 08:06:22
btw,你可以使用'WHERE標記IN(「class」,「new」,「pack」)' – abatishchev 2010-06-21 08:11:02