简体   繁体   中英

MySQL GROUP_CONCAT and JOIN

Media table:

CREATE TABLE $media_table (
            `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
            `title` varchar(300) DEFAULT NULL,
            `options` longtext DEFAULT NULL,
            PRIMARY KEY (`id`)
}

Table example:

id    title     options
--------------------------
1     video      ...         
2     video      ...         
3     audio      ... 
   

Category table:

CREATE TABLE $media_taxonomy_table ( 
            `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
            `type` varchar(15) DEFAULT NULL,
            `title` varchar(300) DEFAULT NULL,
            `media_id` int(11) unsigned DEFAULT NULL,
            PRIMARY KEY (`id`),
            INDEX `media_id` (`media_id`)
}

type can be 'category' or 'tag' (I guess this could be enum)

Table example:

id    type         title      media_id
---------------------------------------
1     category      rock         1  
2     category      trance       1 
3     category      trance       2 
4     category      rock         3
5     tag           silent       1
5     tag           loud         1
6     tag           foo          2

I am trying to GROUP_CONCAT on both category and tag from $media_taxonomy_table.

This query will return GROUP_CONCAT with only category

SELECT mt.id, mt.title, mt.options, GROUP_CONCAT(mtt.title ORDER BY mtt.title ASC SEPARATOR ', ') as category 
    FROM $media_table as mt
    LEFT JOIN $media_taxonomy_table as mtt 
    ON mt.id = mtt.media_id AND mtt.type='category'
    WHERE playlist_id = %d 
    GROUP BY mt.id

Results, received:

id    title         options     category
----------------------------------------
1     video           ...       rock, trance  
2     video           ...       trance  
3     audio           ...       rock

expected (I need tag as well):

id    title         options     category          tag
--------------------------------------------------------------
1     video           ...       rock, trance      silent, load
2     video           ...       trance            foo
3     audio           ...       rock

You can do conditional aggregation:

SELECT 
    mt.id, 
    mt.title, 
    mt.options, 
    GROUP_CONCAT(CASE WHEN mtt.type = 'category' THEN mtt.title END ORDER BY mtt.title SEPARATOR ', ') as categories,
    GROUP_CONCAT(CASE WHEN mtt.type = 'tag'      THEN mtt.title END ORDER BY mtt.title SEPARATOR ', ') as tags
FROM $media_table as mt
LEFT JOIN $media_taxonomy_table as mtt ON mt.id = mtt.media_id
WHERE mt.playlist_id = %d 
GROUP BY mt.id, mt.title, mt.options

Seems you need another group_concat so you need another join

SELECT mt.id
    , mt.title
    , mt.options
    , GROUP_CONCAT(mtt.title ORDER BY mtt.title ASC SEPARATOR ', ') as category 
    , GROUP_CONCAT(mtt2.title ORDER BY mtt.title ASC SEPARATOR ', ') as tag 
FROM $media_table as mt
LEFT JOIN $media_taxonomy_table as mtt 
ON mt.id = mtt.media_id AND mtt.type='category'
LEFT JOIN $media_taxonomy_table as mtt2 
ON mt.id = mtt.media_id AND mtt.type='tag'
WHERE playlist_id = %d 
GROUP BY mt.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM