I have three database tables.
One with items, one with tags and one with items_has_tags.
Tags
CREATE TABLE `tags` (
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(30) NOT NULL
) COMMENT='';
INSERT INTO `tags` (`id`, `name`) VALUES
(1, 'tag one'),
(2, 'tag two'),
(3, 'tag three'),
(4, 'tag four'),
(5, 'tag five');
Items
CREATE TABLE `items` (
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(30) NOT NULL
) COMMENT='';
INSERT INTO `items` (`id`, `name`) VALUES
(1, 'first item'),
(2, 'second item'),
(3, 'third item'),
(4, 'fourth item');
Item has tag
CREATE TABLE `item_has_tag` (
`item_id` int NOT NULL,
`tag_id` int NOT NULL,
) COMMENT='';
INSERT INTO `item_has_tag` (`item_id`, `tag_id`) VALUES
(1, 2),
(1, 3),
(1, 4),
(2, 1),
(2, 3),
(2, 4),
(3, 2),
(3, 3),
(3, 4),
(1, 2),
(4, 3),
(4, 4),
(4, 5);
I want to only select items that have all the specific tags, such as the items that item_has_tag.tag_id = 1 AND item_has_tag.tag_id = 2 AND item_has_tag.tag_id = 3 for example.
edit
The amount of specific tags varies from 1 to 4-5.
edit It is the items list that needs to be returned.
Would you like to help me?
tags.name IN ('dog', cat', bird')
?