如何添加时间变量?
我想对每首歌曲的持续时间求和并返回结果(ret
变量)。问题是我找不到添加时间变量的方法。如何实现这一目标?
$$
declare
ret time;
listeners record;
duration record;
vc varchar;
listener_id int;
begin
ret:='00:00:00';
for listeners in select * from listener loop
if listeners.nick=$1 then
listener_id :=listeners.id;
for duration in select song.duration as dur from playlist, song where song.id=playlist.song_id and listeners.id=playlist.listener_id loop
ret := ret + duration.dur; //how to add time variables?
end loop;
end if;
end loop;
return ret;
end;
$$
I want to sum every song duration and return the outcome (ret
variable). The problem is that I can't find a way to add time variables. How to achieve that?
$
declare
ret time;
listeners record;
duration record;
vc varchar;
listener_id int;
begin
ret:='00:00:00';
for listeners in select * from listener loop
if listeners.nick=$1 then
listener_id :=listeners.id;
for duration in select song.duration as dur from playlist, song where song.id=playlist.song_id and listeners.id=playlist.listener_id loop
ret := ret + duration.dur; //how to add time variables?
end loop;
end if;
end loop;
return ret;
end;
$
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用数据类型
interval
来表示时间间隔。但是你还没有学习SQL:编写一个连接而不是实现嵌套循环的过程代码:我假设
utwor
和song
是相同的,否则你的代码不会使很有道理。You would use data type
interval
for time intervals. But you have yet to learn SQL: write a join instead of procedural code implementing a nested loop:I assume that
utwor
andsong
are the same, otherwise your code does not make much sense.