So I have a 3 tables that I select from to create a selection that looks like this:
The SQL statement used to get this selection (from 3 tables):
SELECT games.title as title, taggings.taggable_id as game_id, tags.name as category, tags.id
FROM taggings, tags, games
WHERE taggings.tag_id = tags.id and games.id = taggings.taggable_id
What I want to do is to make the selection combine all the categories fields. So there would only be one row for superorbit.io and it would contain a category field with "2d Shooter, Asteroids, Free For All, etc". Very much like what this guy was trying to do: How to concatenate strings of a string field in a PostgreSQL 'group by' query?
I'm trying to do that exact same thing, but with little luck. I think the major difference between my case is and his is that I'm working with 3 tables here (taggings, tags and games). Here's the query that I have so far (using the string_agg function) but it doesn't seem to concatenate the categories like you would expect it to. In fact, the results look almost the same as the first code.
SELECT games.title as title, taggings.taggable_id as game_id, string_agg(tags.name, ', ') , tags.id
FROM taggings, tags, games
WHERE taggings.tag_id = tags.id and games.id = taggings.taggable_id GROUP BY tags.name,games.title,taggings.taggable_id,tags.id
What am I doing wrong? I am using Postgres 9.6.7.