对于那些一直在用这个twitter-clone帮助我的人,谢谢!!在你的帮助下,我成功地完成了大部分工作,最后到了追随者功能的最后几步。
现在,我有一个包含以下字段的数据集:用户名,推文,日期
数据示例如下:
Username Tweet Date
kenny hi! 2011-10-07 19:07:00
stan hello 2011-10-05 18:07:00
kenny looks like rain 2011-10-05 17:07:00
stan hello 2011-10-05 14:07:00
cartman authoritay! 2010-10-05 14:07:00
我一直在努力学习SQL语句,该语句会产生一个数据集,其中每个用户只能使用他们最新的推文出现一次。因此,基于上述内容,看起来像这样:
Username Tweet Date
kenny hi! 2011-10-07 19:07:00
stan hello 2011-10-05 18:07:00
cartman authoritay! 2010-10-05 14:07:00
我一直在谷歌搜索sql搜索并尝试过COUNT,DISTINCT,MAX的变种,但无济于事。任何帮助将不胜感激!谢谢!
答案 0 :(得分:6)
select d1.username, d1.tweet, d1.date from data d1 where d1.date =
(select max(d2.date) from data d2 where d1.username = d2.username)
答案 1 :(得分:4)
它不会仅仅通过
起作用select distinct username, tweet, date order by date desc
(这是MSSQL语法)
掌握新数据:
SELECT DISTINCT tweets.username, content, date from tweets
WHERE user_id IN (
SELECT users.id FROM users
INNER JOIN user_users ON users.id = user_users.followed_user_id
WHERE user_users.user_id = 1)
ORDER BY date desc
答案 2 :(得分:1)
SELECT f.*
FROM (
SELECT Username, MAX(`Date`) as maxval
FROM table GROUP BY Username
) AS x INNER JOIN table AS f
ON f.Username = x.Username AND f.`Date`= x.maxval