Using GROUP_CONCAT on subquery in MySQL

前端 未结 5 1704
我在风中等你
我在风中等你 2021-02-02 06:28

I have a MySQL query in which I want to include a list of ID\'s from another table. On the website, people are able to add certain items, and people can then add those items to

相关标签:
5条回答
  • 2021-02-02 06:30

    I think you may have the "userid = itemid" wrong, shouldn't it be like this:

    SELECT ITEMS.id,GROUP_CONCAT(FAVOURITES.UserId) AS IdList
    FROM FAVOURITES 
    INNER JOIN ITEMS ON (ITEMS.Id = FAVOURITES.ItemId OR FAVOURITES.UserId = ITEMS.Creator)
    WHERE ITEMS.Id = $someid
    GROUP BY ITEMS.ID
    
    0 讨论(0)
  • 2021-02-02 06:31

    The purpose of GROUP_CONCAT is correct but the subquery is unnecessary and causing the problem. Try this instead:

    SELECT ITEMS.id,GROUP_CONCAT(FAVOURITES.UserId)
    FROM FAVOURITES INNER JOIN ITEMS ON ITEMS.Id = FAVOURITES.ItemId
    WHERE ITEMS.Id = $someid
    GROUP BY ITEMS.ID
    
    0 讨论(0)
  • 2021-02-02 06:31

    Yes, soulmerge's solution is ok. But I needed a query where I had to collect data from more child tables, for example:

    • main table: sessions (presentation sessions) (uid, name, ..)
    • 1st child table: events with key session_id (uid, session_uid, date, time_start, time_end)
    • 2nd child table: accessories_needed (laptop, projector, microphones, etc.) with key session_id (uid, session_uid, accessory_name)
    • 3rd child table: session_presenters (presenter persons) with key session_id (uid, session_uid, presenter_name, address...)

    Every Session has more rows in child tables tables (more time schedules, more accessories)

    And I needed to collect in one collection for every session to display in ore row (some of them):

    session_id | session_name | date | time_start | time_end | accessories | presenters
    

    My solution (after many hours of experiments):

    SELECT sessions.uid, sessions.name,
        ,(SELECT GROUP_CONCAT( `events`.date SEPARATOR '</li><li>') 
                FROM `events` 
                WHERE `events`.session_id = sessions.uid ORDER BY `events`.date) AS date
        ,(SELECT GROUP_CONCAT( `events`.time_start SEPARATOR '</li><li>') 
                FROM `events` 
                WHERE `events`.session_id = sessions.uid ORDER BY `events`.date) AS time_start
        ,(SELECT GROUP_CONCAT( `events`.time_end SEPARATOR '</li><li>') 
                FROM `events` 
                WHERE `events`.session_id = sessions.uid ORDER BY `events`.date) AS time_end
        ,(SELECT GROUP_CONCAT( accessories.name SEPARATOR '</li><li>') 
                FROM accessories 
                WHERE accessories.session_id = sessions.uid ORDER BY accessories.name) AS accessories
        ,(SELECT GROUP_CONCAT( presenters.name SEPARATOR '</li><li>') 
                FROM presenters
                WHERE presenters.session_id = sessions.uid ORDER BY presenters.name) AS presenters
    
        FROM sessions
    

    So no JOIN or GROUP BY needed. Another useful thing to display data friendly (when "echoing" them):

    • you can wrap the events.date, time_start, time_end, etc in "<UL><LI> ... </LI></UL>" so the "<LI></LI>" used as separator in the query will separate the results in list items.

    I hope this helps someone. Cheers!

    0 讨论(0)
  • 2021-02-02 06:38

    OP almost got it right. GROUP_CONCAT should be wrapping the columns in the subquery and not the complete subquery (I'm dismissing the separator because comma is the default):

    SELECT i.*,
    (SELECT GROUP_CONCAT(userid) FROM favourites f WHERE f.itemid = i.id) AS idlist
    FROM items i
    WHERE i.id = $someid
    

    This will yield the desired result and also means that the accepted answer is partially wrong, because you can access outer scope variables in a subquery.

    0 讨论(0)
  • 2021-02-02 06:47

    You can't access variables in the outer scope in such queries (can't use items.id there). You should rather try something like

    SELECT
        items.name,
        items.color,
        CONCAT(favourites.userid) as idlist
    FROM
        items
    INNER JOIN favourites ON items.id = favourites.itemid
    WHERE
        items.id = $someid
    GROUP BY
        items.name,
        items.color;
    

    Expand the list of fields as needed (name, color...).

    0 讨论(0)
提交回复
热议问题