select group_concat vs group_concat on left join

nate

I'm analyzing a few queries and have noticed some interesting results when trying to extract multiple fields from different tables based on a user_id. Consider the following tables:

+------------------------+------------------+------+-----+---------+----------------+
| Field                  | Type             | Null | Key | Default | Extra          |
+------------------------+------------------+------+-----+---------+----------------+
| subscription_id        | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| user_id                | int(11) unsigned | YES  | MUL | NULL    |                |
| expires                | int(11) unsigned | YES  | MUL | NULL    |                |
| created                | int(11) unsigned | YES  | MUL | NULL    |                |
| modified               | int(11)          | YES  |     | NULL    |                |
+------------------------+------------------+------+-----+---------+----------------+

I want to GROUP_CONCAT all of a user's subscriptions when fetching users. Currently this method works for one field:

SELECT u.id,
(
    SELECT GROUP_CONCAT(s.subscription_id)
    FROM subscriptions s
    WHERE s.user_id = u.id
) AS subscription_ids
FROM users u
LIMIT 10

And I can add all of the fields with different SELECT GROUP_CONCATs. However this method is very slow with any real number of rows due to MySQL joining the subscriptions table for every field. Is there a way that I can GROUP_CONCAT all of the fields at once when listing users?

I've tried a LEFT JOIN method:

SELECT
    u.id AS user_id,
    GROUP_CONCAT(s.subscription_id) AS subscription_ids
FROM users u
LEFT JOIN subscriptions s
    ON s.user_id = u.id
LIMIT 10

However, that appears to concatenate all of the subscription_ids. I'm a bit stumped why the LEFT JOIN would do that. But I'm surprised there doesn't appear to be an easy way to concatenate a list of fields from a different table.

J A

The GROUP_CONCAT is an aggregate function and therefore needs to be applied with GROUP BY clause.

SELECT
    u.id AS user_id, GROUP_CONCAT(s.subscription_id) AS subscription_ids
FROM 
    users u
    LEFT JOIN subscriptions s ON s.user_id = u.id
GROUP BY
    u.id
LIMIT 10

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

select multiselect values using group_concat and left join

From Dev

Mysql with Group by + IF + Left Join + Group_Concat

From Dev

GROUP_CONCAT with LEFT JOIN condition?

From Dev

How can I combine GROUP_CONCAT and LEFT JOIN?

From Dev

SQL left join and group_concat returns duplicate data

From Dev

Data.table left join and aggregate/concatenate/group_concat

From Dev

Getting specific in an INNER JOIN mysqli select statement with group_concat

From Dev

MySQL group_concat with join

From Dev

UPDATE with JOIN and GROUP_CONCAT

From Dev

Group_Concat with left join is slowing down. I need all rows in left table as well

From Dev

MySQL group_concat with select inside select

From Dev

mysql request using join and group_concat

From Dev

Mysql JOIN with GROUP_CONCAT in a complex query

From Dev

GROUP_CONCAT with join resulting in double records

From Dev

mysql query join group_concat

From Dev

join query with group_concat, duplicate values

From Dev

mysql request using join and group_concat

From Dev

GROUP_CONCAT with join resulting in double records

From Dev

mysql inner join group_concat mysql

From Dev

Using Mysql GROUP_CONCAT in JOIN Query

From Dev

How to filter the GROUP_CONCAT in my SELECT

From Dev

Select query with GROUP_CONCAT in mysql

From Dev

SQL Query, Group_Concat of multiple left joins and nested queries

From Dev

SQL Query, Group_Concat of multiple left joins and nested queries

From Dev

Codeigniter Multiple Join Not Getting Sub Cat "Name" with Group_Concat

From Dev

Converting MySQL code to Access: GROUP_CONCAT and a triple JOIN

From Dev

Double results in GROUP_CONCAT with double JOIN in MySQL

From Dev

MySql duplicated values in a join using GROUP_CONCAT

From Dev

mysql self join with group_concat and without duplicates

Related Related

  1. 1

    select multiselect values using group_concat and left join

  2. 2

    Mysql with Group by + IF + Left Join + Group_Concat

  3. 3

    GROUP_CONCAT with LEFT JOIN condition?

  4. 4

    How can I combine GROUP_CONCAT and LEFT JOIN?

  5. 5

    SQL left join and group_concat returns duplicate data

  6. 6

    Data.table left join and aggregate/concatenate/group_concat

  7. 7

    Getting specific in an INNER JOIN mysqli select statement with group_concat

  8. 8

    MySQL group_concat with join

  9. 9

    UPDATE with JOIN and GROUP_CONCAT

  10. 10

    Group_Concat with left join is slowing down. I need all rows in left table as well

  11. 11

    MySQL group_concat with select inside select

  12. 12

    mysql request using join and group_concat

  13. 13

    Mysql JOIN with GROUP_CONCAT in a complex query

  14. 14

    GROUP_CONCAT with join resulting in double records

  15. 15

    mysql query join group_concat

  16. 16

    join query with group_concat, duplicate values

  17. 17

    mysql request using join and group_concat

  18. 18

    GROUP_CONCAT with join resulting in double records

  19. 19

    mysql inner join group_concat mysql

  20. 20

    Using Mysql GROUP_CONCAT in JOIN Query

  21. 21

    How to filter the GROUP_CONCAT in my SELECT

  22. 22

    Select query with GROUP_CONCAT in mysql

  23. 23

    SQL Query, Group_Concat of multiple left joins and nested queries

  24. 24

    SQL Query, Group_Concat of multiple left joins and nested queries

  25. 25

    Codeigniter Multiple Join Not Getting Sub Cat "Name" with Group_Concat

  26. 26

    Converting MySQL code to Access: GROUP_CONCAT and a triple JOIN

  27. 27

    Double results in GROUP_CONCAT with double JOIN in MySQL

  28. 28

    MySql duplicated values in a join using GROUP_CONCAT

  29. 29

    mysql self join with group_concat and without duplicates

HotTag

Archive