我试图运行此查询;
SELECT artist, song, track, album, album_id, art, COUNT(artist) AS artistCount FROM listening_artist GROUP BY artist ORDER BY COUNT(artist) DESC
哪种方法很好,但我希望它只使用数据库中的30个结果而不是完整数据库。
我已尝试过这个
SELECT artist, song, track, album, album_id, art, COUNT(artist) AS artistCount FROM listening_artist LIMIT 30 GROUP BY artist ORDER BY COUNT(artist) DESC
什么都不返回。
我不在此之后
SELECT artist, song, track, album, album_id, art, COUNT(artist) AS artistCount FROM listening_artist GROUP BY artist LIMIT 30 ORDER BY COUNT(artist) DESC
因为这个限制为时已晚。
的含义
从过去30首播放的歌曲中选择用户最常听的艺术家。
我的数据库
+------------+----------+---------+---------+------------+-------+
| artist | song | track | album | album_id | art |
+------------+----------+---------+---------+------------+-------+
| T-Swift | 22 | *NULL* | Red | 9282781 | URL |
| | | | | | |
+------------+----------+---------+---------+------------+-------+
感谢任何帮助。谢谢!
答案 0 :(得分:0)
正如评论中已经指出的那样,你的第二个查询没有返回任何结果,因为语法无效。
而是使用子选择来限制结果:
SELECT artist, song, track, album, album_id, art, COUNT(artist) AS artistCount
FROM (
SELECT *
FROM listening_artist
LIMIT 30
) AS recent_listening_artist
GROUP BY artist
ORDER BY COUNT(artist) DESC
在子选择内,您可以添加其他排序,例如在时间戳上播放了一首歌。
您没有指定实际的DBMS,因此您可能需要稍微调整语法(这是MySQL),但它至少应该为您提供正确方向的提示。
请注意,这只会为每位艺术家提供一行,这使得输出中的曲目或专辑有点毫无意义。有关小型演示,另请参阅this MySQL fiddle。