MySQL Forums
Forum List  »  Newbie

Issue with Joining two tables
Posted by: S Blake
Date: July 06, 2017 09:42PM

Hi, I'm trying to merge two tables together. This is what I have.

$item = "SELECT * FROM posts a JOIN postmeta b ON b.post_id = a.ID WHERE post_type='product'";

The layout is, there are items in `posts`, that have values in `postmeta`.

The code I am using works, but the return is a bit messy.

`Postmeta` has 4 columns, `Meta_ID`, `Post_ID, `Meta_Key`, and `Meta_Value`

The problem is there are dozens of entries that relate to a single product. So when I do a return in php, I am getting an individual object per entry in `Postmeta`. As a result, it is returning the same information from `Posts` for each unique return from `Postmeta`.

I'm wondering how you combine all the affilated `Postmeta` entries into one object(or something similar) so the returns from `Post` and `Postmeta` are contained into one single object, as opposed to the way it is doing it right now.


[237] => stdClass Object
(
[ID] => 1657
...
[meta_id] => 2670
[post_id] => 1657
[meta_key] => _featured
[meta_value] => no
)

[238] => stdClass Object
(
[ID] => 1657
...
[meta_id] => 2671
[post_id] => 1657
[meta_key] => _weight
[meta_value] =>
)

[239] => stdClass Object
(
[ID] => 1657
...
[meta_id] => 2672
[post_id] => 1657
[meta_key] => _length
[meta_value] =>
)


Any insight would be appreciated. Thanks!

Options: ReplyQuote


Subject
Written By
Posted
Issue with Joining two tables
July 06, 2017 09:42PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.