【发布时间】:2015-08-03 02:37:14
【问题描述】:
我需要一些 sql/postgres 大师的帮助。我想要完成的是我有一张tracks、genres 和playlists 的表格。我需要从表tracks 中选择它是否已经存在于track_playlists 中或不存在。我已经做过了。这是当前代码。
SELECT tr.*, MAX(tp.played) as last FROM tracks as tr
LEFT JOIN track_genres as tg ON tr.trk_id = tg.trk_id
LEFT JOIN track_playlists as tp ON tr.trk_id = tp.trk_id
AND tp.mem_id=12345 AND tp.chn_id=9
AND tp.played > EXTRACT(epoch from NOW() - INTERVAL '4 hours')
AND (tr.artist != tp.artist AND tp.played > EXTRACT(epoch from NOW() - INTERVAL '1 hours'))
WHERE tg.rotation=2 GROUP BY tr.trk_id ORDER BY last ASC LIMIT 6
示例场景:
表tracks
trk_id | artist | title
-------------------------
1 | artist1 | title1
2 | artist1 | title2
3 | artist2 | title1
4 | artist3 | title1
5 | artist4 | title1
6 | artist5 | title1
7 | artist1 | title3
8 | artist6 | title1
表track_genres
gen_id | trk_id | rotation
--------------------------
1 | 1 | 9
2 | 2 | 9
3 | 3 | 2
4 | 4 | 3
5 | 5 | 9
6 | 6 | 2
7 | 7 | 10
8 | 8 | 9
表track_playlists
pla_id | mem_id | trk_id | chn_id | artist | title | played
-------------------------------------------------------------
这是我正在处理的表格。目前,当用户启动时,他们的track_playlist 是空的。当他们选择一种流派时,它应该填充此表,但不是一次全部填充,因此是 Limit 子句,并且它需要根据此表中是否已经有歌曲为用户选择歌曲。如果没有任何歌曲,那么它会通过我正在尝试的选择查询来选择歌曲。随着歌曲的播放,当它要提取新歌曲时,它需要查看track_playlist 中已经存在哪些歌曲。如果歌曲已经在播放列表中,那么它应该跳过它,只为这个用户提取尚未在播放列表中的歌曲。一旦 all 歌曲最终出现在用户的播放列表中,它只需要检查 played timestamp 以查看该歌曲的最后播放时间是否大于 4 小时,并且它是 @987654341 的最旧曲目@(最近最少播放)而不是最近 1 小时内播放的艺术家。
我希望这更清楚一点。
我用这个答案来帮助我查询是否为空。
sql update data in 2nd db from 1st db only where table exists
【问题讨论】:
-
请给我们一些示例数据以及您的预期结果,以便进一步澄清;)。
-
你的问题有点难以解读,这可能是为什么还没有人破解它的原因。如果您提供示例表、数据和结果,甚至是 sqlfiddle,会更容易。
---现在我想说 and 是最近最少播放的曲目 不清楚并且没有反映在您的查询中,而且AND (tr.artist != tp.artist AND tp.played > EXTRACT(epoch from NOW() - INTERVAL '1 hours'))看起来不正确 - 您已经加入tr到tp上trk_id,但又说艺术家一定不一样?我认为您可能将太多东西组合成一个连接。
标签: sql postgresql postgresql-9.1