在我的测试程序中,用户输入他们想要播放列表的总时间,然后生成不再是给定时间的播放列表。 但是,我遇到了一些问题,因为我无法弄清楚如何在播放列表中累计歌曲的长度,然后确保查询不再是给定的时间。请有人能帮助我吗? 这是我的代码:
c.execute ('SELECT (TOTAL(Length) AS TotalSongLength, "Song Name", "Artist", "Genre", "Album", "Year") FROM Songs WHERE TotalSongLength <= "{0}"'.format(TotalSongLengthInput))
在代码是用户输入时间之前发生的所有事情。在表格中,歌曲以MM:SS
格式存储,其输入自动转换为该格式。我只是正在努力解决上述问题^。
答案 0 :(得分:1)
你可以使用sum和group by
并过滤您应该使用的结果(对于汇总结果)而不是
SELECT sum(Length) AS TotalSongLength, "Song Name", "Artist", "Genre", "Album", "Year"
FROM Songs
GROUP BY "Song Name", "Artist", "Genre", "Album", "Year"
HAVING TotalSongLength <= "{0}"'
答案 1 :(得分:0)
无法回复评论,但scaisEdge可以为您提供所需的答案
桌子上的数据类型会强制你为你所看到的一切做几秒钟的转换,如下所示:
SELECT
SongName
, strftime('%MM', Length) * 60 + strftime('%SS',Length) AS TotalSeconds
, Length
FROM Songs
GROUP BY SongName, Length
HAVING TotalSeconds <= "{0}"
然后你必须将用户输入解析为秒以及与TotalSeconds进行比较
答案 2 :(得分:0)
我认为以下内容可能适合: -
SELECT
CASE
WHEN seconds > 0 AND seconds < 300
THEN 'Playlist OK'
ELSE 'Playlist too long or not long enough'
END AS result
FROM
(SELECT sum((substr(length,1,2) * 60) + substr(length,4,2)) as seconds
FROM songs);
使用以下方法测试:
DROP TABLE IF EXISTS songs;
CREATE TABLE IF NOT EXISTS songs ("Song Name" TEXT,"Artist" TEXT, "Genre" TEXT, "Album" TEXT, "Year", length TEXT);
INSERT INTO songs VALUES ('My Song','Fred','Rock','Fred Rocks','1974', '03:11');
INSERT INTO songs VALUES ('Another Song','Bert','Pop','Bert sings','2010', '02:59');
SELECT
CASE WHEN seconds > 0 AND seconds < 300 THEN 'Playlist OK' ELSE 'Playlist too long or not long enough' END AS result
FROM (SELECT sum((substr(length,1,2) * 60) + substr(length,4,2)) as seconds FROM songs)
然后: -
如果您想要时间以秒为单位,那么: -
SELECT seconds,
CASE
WHEN seconds > 0 AND seconds < 300
THEN 'Playlist OK'
ELSE 'Playlist too long or not long enough'
END AS result
FROM
(SELECT sum((substr(length,1,2) * 60) + substr(length,4,2)) as seconds FROM songs);
这不是我想要的。而不是说如果 播放列表低于某个时间或其他任何时间。我想要它显示所有 可以加在一起的歌曲,无需浏览用户输入
我相信以下内容会做到这一点(最后一行的1000是时间限制): -
SELECT *
FROM
(
SELECT SUM(a.seconds) AS summed,
b."Song Name",
b."Artist",
b."Genre",
b."Album",
b."Year",
b.length,
b.seconds
FROM (
SELECT *,
(substr(length,1,2) * 60) + substr(length,4,2) AS seconds
FROM songs ORDER BY length) AS a,
(
SELECT *,
(substr(length,1,2) * 60) + substr(length,4,2) AS seconds
FROM songs ORDER BY length) AS b
WHERE b."Song Name" <= a."Song Name"
GROUP BY b."Song Name" ORDER BY summed
)
WHERE summed <= 1000
根据表格: -
运行上述结果: -
限制= 1000: -
限制= 10000: -
限制= 600: -