How to get multiple field values ​​in one field in mysql?

I have 3 tables, post_tbl, tag_tbl, post_tag_tbl, and as you know we can join them to get the tag name. but I want to show 10 posts each time for pagination and show the title of the tags. we can do this with a query to get 10 posts and then for each whit post id get the tags, but this is really inefficient. is there any way to get mysql fields as an array, for example field tags contain all post tag names.

post_id      post_name     tags
1            post one      tag1,tag2,tag3
2            post two      tag3,tag2,tag5

      

What's the best solution for this kind of problem?

+3


source to share


2 answers


GROUP_CONCAT (column_name)

This function returns the result of a string with concatenated non-NULL values ​​from a group. It returns NULL if there are no non-NULL values.



select GROUP_CONCAT(tags_name) as tag
from tags where tag_post_id = Xyz

      

+1


source


this article is perfect for problems like this https://www.percona.com/blog/2013/10/22/the-power-of-mysqls-group_concat/#comment-10865677



0


source







All Articles