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.
我想要,如果可能的话,正如你在图片上看到的,得到这一行的结果。如果不可能,那么2行结果,所以稍后我可以循环,从2行得到2个类别。
I can't figure out right SQL_QUERY to this.
我找不出正确的SQL_QUERY。
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?
或者用这种方式获取数据是不好的,我应该用2个查询而不是1个查询?
1 个解决方案
#1
2
You could use group_concat and group by
您可以使用group_concat和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
#1
2
You could use group_concat and group by
您可以使用group_concat和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