如何组合2个这样的MySQL表? (多对一关系)

时间:2012-06-17 08:09:49

标签: mysql

我有这样的UserComments表:

1 | Frank | hello world
2 | Jane | Hi there
3 | Frank | this is my comments
4 | Frank | I think I need some sleep
5 | Jason | I need to buy new MacBook
6 | Jane | Please invite my new Blackberry PIN

另一方面,我有FriendList表包含:

1 | Jason
2 | Jane

假设我的朋友ID总是在1和5之间。

既然弗兰克不是我的朋友,我也无法看到他的评论。如何组合这样的表(ORDER BY UserComments.ID DESC):

1 | Jane | Please invite my new Blackberry PIN
2 | Jason | I need to buy new MacBook
3 | Jane | Hi there

感谢。

2 个答案:

答案 0 :(得分:1)

试试这个:

    SELECT A.ID, B.UserName, B.Comment 
      FROM FriendList A
INNER JOIN UserComments B ON A.ID = B.ID
  ORDER BY A.ID DESC

答案 1 :(得分:1)

试试这个::

Select 
* 
from 
user_comments inner join friend_List on (join criteria)
where user_Id = ? order by user_comments.id desc