我有两个表:聚合函数冲突
users:
___________________________
|user_id | username |
|_______________|___________|
| 1 | Dolly |
| 2 | Didi |
|_______________|___________|
forum:
_____________________________________________________________
|match_static_id| comment | timpstamp | user_id |
|_______________|___________|______________________|__________|
| 1 | Hi | 2013-07-10 12:15:03 | 2 |
| 1 | Hello | 2013-07-09 12:14:44 | 1 |
|_______________|___________|______________________|__________|
此查询工作正常,它使用只是THW forum
表:
SELECT forum.match_static_id,
count(forum.match_static_id) 'comments_no', max(forum.timestamp)'timestamp'
FROM forum
GROUP BY forum.match_static_id
Order BY timestamp DESC
但是,下面的查询使用两个表:
SELECT forum.match_static_id,
count(forum.match_static_id) 'comments_no', max(forum.timestamp)'timestamp', users.username
FROM forum
INNER JOIN users on users.id = forum.user_id
GROUP BY forum.match_static_id
在这里我想得到的最大用户(时间戳),但我得到了错误的用户可以任何身体给我一个线索,请吗? ORDER BY的时间戳降序
你想的'MAX(时间戳)'的foreach'match_static_id '或什么?那么'users.username'你想为每个分组的'match_static_id'选择哪个用户名呢? –
我想要max(timestamp)的注释的用户名 – Basel
这是一个常见问题 – Strawberry