Adrianno Barello
Adrianno Barello

Reputation: 137

Can't figure out query for this tables and relations

My tables, relations and wanted results: My tables, relations and wanted results

I want if it is possible, as you can see on the picture, to get this 1 row result. If it is not possible then 2 rows result so later I can loop through to get 2 categories from 2 rows.

I can't figure out right SQL_QUERY to this.

I don't know how create query to have wanted results.

How this query should look like?

Or maybe it is bad way to get data like this, and I should make this in 2 queries instead of 1 query?

Upvotes: 1

Views: 40

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

You could use group_concat and group by

select p.*, group_concat(c.nazwa)
from posts p
inner join posts_categories pc on pc.id_posta = p.id
inner join categories c on c.id = pc.id_kategorii
group by p.id

Upvotes: 2

Related Questions