0

I am a bit confused about using GROUP_CONCAT in MySQL.

To explain, I am working on a listings directory, sort of like Yelp for example. A user filters different criteria and is returned with different places.

For example, if a user wants to see all places that have been categorized (or tagged) as "Restaurant", or maybe "Lounge" then they will see only those places, but also they should be presented with any other tagged categories.

To do this, I have built some tables in my database. The main tables that relate to this question are the tables places and categories and place_category_rel as such:

places table:

+----+------------------+
| id | name             | 
+----+------------------+
| 1  | Johns Restaurant |
+----+------------------+
| 2  | Jims Place       |
+----+------------------+
| 3  | Cafe Luna        |
+----+------------------+

place_category_rel table

+----------+-------------+
| place_id | category_id |
+----------+-------------+
| 1        | 1           |
+----------+-------------+
| 1        | 2           |
+----------+-------------+

categories table

+----+------------+
| id | name       |
+----+------------+
| 1  | Lounge     |
+----+------------+
| 2  | Restaurant |
+----+------------+
| 3  | Night Club |
+----+------------+

To obtain all places I use the following query:

SELECT p.id,
       p.name,
       group_concat(DISTINCT cat.name separator ',') as categories
FROM places p
LEFT JOIN places_categories_rel rel ON p.id=rel.place_id
LEFT JOIN categories cat ON cat.id=rel.category_id
GROUP BY p.id

Which gets me this result set:

+----+------------------+--------------------+
| id | name             | categories         |
+----+------------------+--------------------+
| 1  | Johns Restaurant | Lounge,Restaurant  |
+----+------------------+--------------------+
| 2  | Jims Place       | Null               |
+----+------------------+--------------------+
| 3  | Cafe Luna        | Null               |
+----+------------------+--------------------+

The above works great. However, when I add a WHERE clause to filter down on places that are categorized as "Lounge" , I lose the other category:

SELECT p.id,
       p.name,
       group_concat(DISTINCT cat.name separator ',') as categories
FROM places p
LEFT JOIN places_categories_rel rel ON p.id=rel.place_id
LEFT JOIN categories cat ON cat.id=rel.category_id
WHERE cat.name = 'Lounge' 
GROUP BY p.id

Result:

+----+------------------+------------+
| id | name             | categories |
+----+------------------+------------+
| 1  | Johns Restaurant | Lounge     |
+----+------------------+------------+

what I would like is this result set instead:

+----+------------------+--------------------+
| id | name             | categories         |
+----+------------------+--------------------+
| 1  | Johns Restaurant | Lounge, Restaurant |
+----+------------------+--------------------+

Can someone point me in the right direction? Thanks.

2 Answers 2

1

If I'm understanding correctly, you are trying to get all categories for a given place if they have a specific category. Assuming so, one option is to use exists:

SELECT p.id,
       p.name,
       group_concat(DISTINCT cat.name separator ',') as categories
FROM places p
   LEFT JOIN places_categories_rel rel ON p.id=rel.place_id
   LEFT JOIN categories cat ON cat.id=rel.category_id
WHERE EXISTS (
   SELECT 1
   FROM places_categories_rel pcr 
       JOIN categories c ON c.id=pcr.category_id
   WHERE name = 'Lounge'  AND p.id=pcr.place_id
)
GROUP BY p.id

Not sure you need outer joins in this case either -- probably would work the same with inner joins.

3
  • Thanks for the answer. Can you please explain how the WHERE EXISTS section works? what does the line SELECT 1 actually do?
    – CodeGodie
    Apr 3, 2016 at 4:03
  • @CodeGodie -- exists is used how it sounds -- does a given record exist matching specific criteria. And select 1 is arbitrary -- this query is saying if a given place_id (based on the where criteria of the subquery) has a category with Lounge, then return true. So for any matching places, all categories will be returned.
    – sgeddes
    Apr 3, 2016 at 4:05
  • Gotcha, thank you for the explanation. So far I like your answer but I will wait for other answers to get different perspectives. Ill keep you posted.
    – CodeGodie
    Apr 3, 2016 at 4:09
1

When using a LEFT JOIN, conditions on all but the first table should be in the ON clause:

SELECT p.id,
       p.name,
       group_concat(DISTINCT cat.name separator ',') as categories
FROM places p LEFT JOIN
     places_categories_rel rel
     ON p.id = rel.place_id LEFT JOIN
     categories cat
     ON cat.id = rel.category_id AND cat.name = 'Lounge' 
GROUP BY p.id;

Remember: A LEFT JOIN returns all rows in the first table, regardless of whether the ON clause returns true, false, or NULL. If the ON condition is not true, then all columns in the second (and subsequent) tables are NULL.

When you test for cat.name = 'Lounge' in the WHERE clause, cat.name is NULL for the rows that do not match -- hence they are filtered out.

EDIT:

If you only want the category of lounge in the results (with others), then just add a having clause:

SELECT p.id,
       p.name,
       group_concat(DISTINCT cat.name separator ',') as categories
FROM places p LEFT JOIN
     places_categories_rel rel
     ON p.id = rel.place_id LEFT JOIN
     categories cat
     ON cat.id = rel.category_id
GROUP BY p.id
HAVING SUM(cat.name = 'Lounge') > 0;

I think this is the simplest approach.

1
  • Thanks for the answer. Unfortunately its not the answer I was looking for. Perhaps I explained myself incorrectly. I want the query to say "only show me the places that have the category of Lounge". The result should be only one row: Johns Restaurant, showing the category Lounge(what we asked for) but also "Restaurant" because its also a category of that place.. Does that make more sense?
    – CodeGodie
    Apr 3, 2016 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.