For context here is a simplified way of my DB Structure
Table definitions and data sample :
CREATE TABLE `screens` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`playlist_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE `playlist` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ;
CREATE TABLE `playlist_slides` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`playlist_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE `resource_tags` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`tag_id` int(11) NOT NULL,
`item_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE `tags` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ;
INSERT INTO `screens` VALUES (1, 2);
INSERT INTO `playlist` VALUES (2);
INSERT INTO `playlist_slides` VALUES (3, 2);
INSERT INTO `resource_tags` VALUES (1, 1, 1);
INSERT INTO `resource_tags` VALUES (2, 2, 1);
INSERT INTO `resource_tags` VALUES (3, 3, 1);
/*INSERT INTO `resource_tags` VALUES (1, 1, 3);
INSERT INTO `resource_tags` VALUES (3, 3, 3); */
INSERT INTO `tags` VALUES (1);
INSERT INTO `tags` VALUES (2);
INSERT INTO `tags` VALUES (3);
I would like to check if all of the tags assigned to the playlist_slides table exists in the tags assigned in screens table.
Currently I tried doing it like this:
SELECT
`media_content_array`.*,
`current_screen_tags`.`screen_tags`
FROM `screens`
RIGHT JOIN (
SELECT
`playlist_slides`.`playlist_id`,
GROUP_CONCAT(
playlist_content_tags.tag_id
ORDER BY playlist_content_tags.tag_id
) AS tag_ids
FROM `playlist_slides`
LEFT JOIN `playlist_content_tags`
ON `playlist_slides`.`id` = `playlist_content_tags`.`item_id`
GROUP BY `playlist_slides`.`playlist_id`
) AS `media_content_array`
ON `media_content_array`.`playlist_id` = `screens`.`playlist_id`
RIGHT JOIN (
SELECT
`screens`.`id`,
GROUP_CONCAT(
resource_tags.tag_id
ORDER BY resource_tags.tag_id
) AS screen_tags
FROM `screens`
LEFT JOIN `resource_tags`
ON `screens`.`id` = `resource_tags`.`item_id`
WHERE `screens`.`id` = ?
GROUP BY `screens`.`id`
) AS `current_screen_tags`
ON `screens`.`id` = `current_screen_tags`.`id`
WHERE `screens`.`id` = ?
AND `media_content_array`.`tag_ids` LIKE CONCAT('%', `current_screen_tags`.`screen_tags`, '%');
as you can see i used
media_content_array.tag_ids LIKE CONCAT('%', media_content_array.tag_ids, '%'
it worked only if tag_ids is on the right sequence.
Not sure how ill do it any other way, please help, thanks!
2
Answers
fiddle
"I would like to check if all of the tags assigned to the playlist_slides table exists in the tags assigned in screens table."
Other answers, whilst correct, have built on your existing query… which is exceedingly complex. You can break down this problem into 2 parts:
Find the tag id of of each playlist_slide (using a join)
Checking which tags do not exist in resource_tags (using not in)
Technically though, you don’t even need to do a join, you can just select the tag_id from resource_tag and use the same condition