已经给出并接受了答案,但我想补充一下。
你想要的是一张有用户的桌子,users
. 在此表中,您存储您的用户信息 ( user_id
, name
)。
在您的Tweets
表中,存储所有用户的所有推文。每行一条推文。我正在使用tweet_id
as 作为PRIMARY KEY
桌子Tweets
。
然后,您可以通过JOIN
像 Dave Swersky 所说的那样在代码中“链接”两者。
例如:
Users
--------------------------
user_id | user_name
--------------------------
123 | 'Terry'
34 | 'Pierre'
Tweets
-----------------------------------------------------------------------
tweet_id | user_id | time | message
-----------------------------------------------------------------------
0 | 123 | 135646 | 'This is a tweet'
1 | 123 | 132646 | 'This is another tweet by Terry'
2 | 34 | 352646 | 'Pierre\'s tweet'
我不确定name
你的Tweets
桌子上有什么用。据我所知,推文没有名称/主题(?)。您不需要将用户名存储在tweets
和users
表中。
如需快速 SQLFiddle,请访问此处:http ://www.sqlfiddle.com/#!2/43492/1/0
加入
SELECT u.user_id, u.name, t.time, t.message, t.time
FROM my_users u
INNER JOIN tweets t ON u.user_id = t.user_id