I have this Tags table
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');
I want to fetch all records where tag matches keywords class
or pack
or new
, along with another field which indicates which of the 3 keywords actually matched with the tag field.
The following query does not give correct results Query 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%"
I have to use the like inside the case. Otherwise complete matching works. The following query works:-
Query 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"
What is wrong with the query 1. Please help.