MYSQL SELECT DISTINCT 歌曲 ID、时间 按时间排序
我的音乐网站主页上有一个当前正在收听的盒子,并且有一个这样的表:
id,userid,songid,time
现在每次用户播放歌曲时,歌曲的 id 都会插入到 Songid 字段中,同时也会插入时间。所以有时我有重复的songid(由不同用户插入)。
现在这是我的 sql 语句:
SELECT DISTINCT songid,time FROM songs ORDER BY time DESC LIMIT 10
它给了我最近听过的十首歌曲,但有时它也会返回重复的数据!我该如何解决这个问题?提前致谢
i have a currently listening box on the homepage of my music website, and i have a table like this:
id,userid,songid,time
now every time a user plays a song, the id of the song is inserted into the songid field and also the time. so sometimes i have duplicate songid (inserted by different users).
Now here is my sql statement:
SELECT DISTINCT songid,time FROM songs ORDER BY time DESC LIMIT 10
that gives me the last ten songs that were listened, however sometimes it returns duplicate data too! how can i fix this problem? Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要聚合
songid
:You'll want to aggregate over
songid
:如果时间是歌曲上次播放的日期时间,您可以根据上次播放时间对 gon 进行分组:
If the time is the datetime the song was last played, you can group the gons with the last time each was played:
创建以下索引:
以便快速运行。
与
GROUP BY Songid
解决方案不同,这不需要对歌曲
进行全表扫描或全索引松散扫描。Create the following indexes:
for this to work fast.
Unlike the
GROUP BY songid
solutions, this does not require a full table scan or a full index loose scan onsongs
.