I'm trying to get the quotes.quote, authors.author and tags.tag from the following table structure,
CREATE TABLE IF NOT EXISTS `authors` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`author` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `quotes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`quote` text COLLATE utf8_unicode_ci NOT NULL,
`author_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `quotes_author_id_foreign` (`author_id`),
);
CREATE TABLE IF NOT EXISTS `quote_tags` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`tag_id` int(10) unsigned NOT NULL,
`quote_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `tags` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`tag` varchar(225) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
);
ALTER TABLE `quotes`
ADD CONSTRAINT `quotes_author_id_foreign` FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`);
But here the problem is, there can be multiple entry in the tags table for each quotes and I want to retrieve it in the comma separated format.
Here is what I have tried so far,
SELECT `quotes`.`id` , `quotes`.`quote` , `authors`.`author` , (
SELECT GROUP_CONCAT( `tag` )
FROM tags
JOIN quote_tags
WHERE quote_tags.tag_id = tags.id
AND quote_tags.quote_id = quotes.id
) as tags
FROM `quotes`
INNER JOIN `authors` ON `authors`.`id` = `quotes`.`author_id`
INNER JOIN `topics`
But I don't want to use sub queries to get that done.
Please help me in building this query without using sub queries.
SELECT `_quotes`.`id` , `_quotes`.`quote` , `_authors`.`author`
,GROUP_CONCAT( `tag` )
FROM `_quotes`
INNER JOIN `_authors` ON `_authors`.`id` = `_quotes`.`author_id`
inner join _tags
JOIN _quote_tags
on _quote_tags.tag_id = _tags.id
AND _quote_tags.quote_id = _quotes.id
INNER JOIN `_topics` group by `_quotes`.`id`,`_tags`.`id`
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.