MySQL JOIN两个表只有一行,每个条目都有一个属性

时间:2016-04-10 09:14:02

标签: mysql join

我的数据库中有两个表,我正在尝试加入:

表格示例"帖子":

id | author | title
21 | 1      | This is my post.
22 | 2      | I am another title.
23 | 3      | And yet another post.

表格示例" usermeta":

meta_id | user_id | meta_key | meta_value
1       | 1       | name     | John Doe
2       | 1       | zip-code | 12345
3       | 1       | status   | premium
4       | 2       | name     | Steve Jobs
5       | 2       | zip-code | 22499
6       | 2       | status   | basic

现在经过最近几个小时的实验,我回到了我的初步查询:

SELECT * FROM posts
LEFT JOIN usermeta ON posts.author = usermeta.user_id
WHERE posts.author = 1 

这给了我以下结果:

id | author | title                 | user_id | meta_key | meta_value |
21 | 1      | This is my post.      | 1       | name     | John Doe   |
21 | 1      | This is my post.      | 1       | zip-code | 12345      |
21 | 1      | This is my post.      | 1       | status   | premium    |

但是我试图解决这个问题:

id | author | title                 | user_id | author_name | author_zip | author_status |
21 | 1      | This is my post.      | 1       | John Doe    | 12345      | premium       |

为了达到上述结果我还能尝试什么?

谢谢!

0 个答案:

没有答案