我在API服务器上创建查询时遇到了一些问题,我只想回复修改后的曲目数据。我有tracks
表,列表很少。
tracks(id, audio_fingerprint, name, creation_date, modified_date)
现在我只想要在其上次提取的时间戳(音频指纹数组和最后提取的时间戳作为API请求参数传递)后更新的曲目。
SELECT * from tracks WHERE (audio_fingerprint, modified_date) IN (Array(audioFingerprint, > lastFetchedTimestamp));
(^^这是无效的查询,仅用于理解)。
由于
答案 0 :(得分:2)
示例数据:
create table tracks (audio_fingerprint text, modified_date date);
insert into tracks values
('a', '2017-01-10'),
('b', '2017-01-10'),
('a', '2017-02-10'),
('b', '2017-02-10'),
('c', '2017-02-01');
将您的参数放在with
查询中,并将其与您的表格连接:
with given_values (fingerprint, last_fetched) as (
values
('a', '2017-01-01'::date),
('b', '2017-02-01')
)
select *
from tracks t
join given_values v
on t.audio_fingerprint = v.fingerprint
and t.modified_date > v.last_fetched;
audio_fingerprint | modified_date | fingerprint | last_fetched
-------------------+---------------+-------------+--------------
a | 2017-01-10 | a | 2017-01-01
a | 2017-02-10 | a | 2017-01-01
b | 2017-02-10 | b | 2017-02-01
(3 rows)
您也可以使用派生表代替CTE:
select *
from tracks t
join (
values
('a', '2017-01-01'::date),
('b', '2017-02-01')
) v(fingerprint, last_fetched)
on t.audio_fingerprint = v.fingerprint
and t.modified_date > v.last_fetched;
答案 1 :(得分:0)
也许LEAD or LAG可以帮到你?