1
我有两个表格,用户和帖子。我试图编写一个查询来查找用户的最新帖子,但我遇到了麻烦。这是迄今为止我所拥有的。获取最近的帖子
select a.username, b.last_post from logins as a join (select login_id, entry as last_post from posts) as b where a.id = b.login_id
+-----------+---------------------+
| username | last_post |
+-----------+---------------------+
| something | 2013-10-08 22:12:00 |
| other | 2013-10-08 22:13:00 |
| test | 2013-10-08 22:13:03 |
| test | 2013-10-08 22:14:20 |
| hello | 2013-10-08 22:12:53 |
| hello | 2013-10-08 22:12:56 |
+-----------+----------+----------+
所以现在last_post
仅仅是它的拉后的时间戳。我如何获得一个表格,只显示这些用户的最新帖子?
谢谢!你能解释在第一个查询中使用“group by”,但不能在第二个查询中使用?顺便说一句,我用了第二个。 – bvpx
它简单地通过'username'对记录进行分组,并且对于每个用户,它使用MAX()获取最新的'entry' –