我遇到了一些我尚未解决的问题。看看下表。
+---+------------+-------------------------------------+
|id | likes | fb_likes | created_time |
+---+------------+-------------------------------------+
|10 | 0 | 0 | 2013-04-07 13:53:36 |
| 9 | 3 | 2 | 2013-04-07 13:17:18 |
| 8 | 2 | 1 | 2013-04-06 21:49:30 |
| 7 | 2 | 0 | 2013-04-06 20:25:24 |
| 6 | 6 | 5 | 2013-04-06 18:52:27 |
| 5 | 3 | 0 | 2013-04-05 21:19:34 |
| 4 | 5 | 2 | 2013-04-05 19:09:13 |
| 3 | 9 | 10 | 2013-04-03 14:18:56 |
| 2 | 0 | 1 | 2013-04-02 14:28:36 |
| 1 | 0 | 0 | 2013-04-02 08:39:32 |
+---+------------+-------------------------------------+
我想选择每天最喜欢的ID。我已经看过很多想要做类似事情的帖子,但还没有找到解决方案。
我的尝试失败(这选择了正确的喜欢,但错误的ID):
SELECT id, MAX(likes+fb_likes) as Tlikes, created_time FROM table GROUP BY DAY(created_time);
我也试过这样的事情: http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html
任何帮助将不胜感激
更新 我忘了添加fb_likes列。我多么愚蠢。
答案 0 :(得分:4)
SELECT a.*
FROM TableName a
INNER JOIN
(
SELECT DATE(created_time) date, MAX(likes) max_likes
FROM TableName
GROUP BY DATE(created_time)
) b ON DATE(a.Created_Time) = b.date AND
a.likes = b.max_Likes
如果您只想显示至少有1票的记录,则需要在外部选择中添加WHERE
子句,例如
答案 1 :(得分:0)
SELECT id, likes, day(created_time)
FROM Likes
WHERE likes =
(SELECT MAX(maxLikes.likes)
FROM Likes maxLikes
WHERE day(maxLikes.created_time) = day(Likes.created_time)
);