I'm trying to figure out how to query my MYSQL Database - unfortunately I'm not good at MYSQL at all...
I have two tables:
posts:
---------------------
| id | name | type |
---------------------
| 1 | mike | type1 |
| 2 | john | type2 |
| 3 | bill | type1 |
---------------------
postmeta:
-------------------------------
| id | post_id | key | value |
-------------------------------
| 1 | 1 | key1 | val1 |
| 2 | 1 | key2 | val2 |
| 3 | 3 | key3 | val3 |
-------------------------------
What I want is some logic like:
When posts.type == type 1
select m.id, m.key, m.value from postmeta m
where post_id === posts.id
And best would be, if I had a $result => $row like this:
array (
id => post.id,
name => post.name,
options => array (
key => value
...
)
)
Here is my query right now. It get's close, but doesn't do the trick yet:
$sql = "SELECT p.id, p.post_title, p.post_content, p.post_type, m.meta_key, m.post_id, m.meta_id, m.meta_value
FROM posts p
JOIN postmeta m ON m.post_id = p.id
WHERE type='type1'
GROUP BY p.id";
What am I missing?
Thanks for your help!
Seb