I have a MySQL DB with 2 tables.
First table:
Posts
ID
post_title
post_content
... etc
And second table with additional data:
Postmeta
ID
Post_ID (FK)
meta_key
meta_value
The data that I need is in the meta_key/value pairs. But this DB organized in the way that each row in the second table only have one piece of information.
For example (second table):
ID Post_ID meta_key meta_value
1 5 info 30
ID Post_ID meta_key meta_value
2 5 additional_info 40
ID Post_ID meta_key meta_value
3 6 info 50
ID Post_ID meta_key meta_value
4 6 additional_info 60
So if I run query
SELECT
posts.id,
posts.post_date,
posts.post_author,
posts.post_content,
posts.post_title,
postmeta.meta_id,
postmeta.meta_key,
postmeta.meta_value
FROM posts INNER JOIN postmeta ON posts.id = postmeta.post_id
WHERE posts.post_type = 'post'
I get back all data that I need but data from first table (ID/post_content etc) repeated many times.
But I only need data from first table once but all additional data from second table related to ID from the first table.
How I should filter all this stuff? May be some php loop?
You can use a mysql group_concat function like this:
The to split out the results in PHP you can easily use an explode function to form an array of the resulting information.