SQLite:总列值,然后与输入

时间:2018-04-05 19:49:45

标签: python sql python-3.x sqlite

在我的测试程序中,用户输入他们想要播放列表的总时间,然后生成不再是给定时间的播放列表。 但是,我遇到了一些问题,因为我无法弄清楚如何在播放列表中累计歌曲的长度,然后确保查询不再是给定的时间。请有人能帮助我吗? 这是我的代码:

c.execute ('SELECT (TOTAL(Length) AS TotalSongLength, "Song Name", "Artist", "Genre", "Album", "Year") FROM Songs WHERE TotalSongLength <= "{0}"'.format(TotalSongLengthInput))

在代码是用户输入时间之前发生的所有事情。在表格中,歌曲以MM:SS格式存储,其输入自动转换为该格式。我只是正在努力解决上述问题^。

3 个答案:

答案 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);
  • 300 是允许的总时间。

测试

使用以下方法测试:

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)

然后: -

  • a)评论所有插页(歌曲),
  • b)评论第一首歌,
  • c)评论第二首歌和
  • d)不评论任何歌曲

测试结果

  • a)播放列表太长或不够长
  • b)播放列表确定
  • c)播放列表确定
  • d)播放列表太长或不够长

如果您想要时间以秒为单位,那么: -

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);
  • 注意如果没有行,那么秒将为null而不是0。

评论: -

  

这不是我想要的。而不是说如果   播放列表低于某个时间或其他任何时间。我想要它显示所有   可以加在一起的歌曲,无需浏览用户输入

我相信以下内容会做到这一点(最后一行的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

根据表格: -

enter image description here

运行上述结果: -

限制= 1000: -

enter image description here

限制= 10000: -

enter image description here

限制= 600: -

enter image description here