SQL 原始查询无法正常工作



我在查询中使用了一个except子句,但我得到了如下错误:

查询的文本格式为:

select songs.id, songs.name, songs.artist, songs.album, songs.albumart, songs."albumartThumbnail", cpr.votes, is_requested from (select id, name, artist, album, albumart, "albumartThumbnail" from (select song_id from core_plsongassociation where playlist_id in (1477)) as sinpl
left join songs on sinpl.song_id=id where explicit=False
) as songs left join
(select song_id, votes, bool_or(thirdpartyuser_id=177) as is_requested from
(select * from core_priorityrequests where client_id=2876 and is_played=False
) as clpr left join core_priorityrequests_third_party_user on clpr.id=priorityrequests_id
group by priorityrequests_id, song_id, votes
) as cpr on songs.id=cpr.song_id 
EXCEPT
(select core_blockedsongs_song.song_id from core_blockedsongs 
join core_blockedsongs_song on core_blockedsongs.id=core_blockedsongs_song.blockedsongs_id where core_blockedsongs.unblock_flag = 'f' and core_blockedsongs.client_id=2870);

上述错误的原因是由于except子句下方的exception上方的查询中的列存在差异,即写在except上方的查询的列数比写在只有1(一(列的except子句之后的查询的多。

有人能建议任何变通方法/解决方案来改进在中编写的查询吗?除了这样我就不必添加其中的所有列吗?

更新1

我的模型更新如下,现在宋领域是一个外键,以前是多对多领域。

class BlockSong(models.Model):
client = models.ForeignKey('Client')
user= models.ForeignKey(settings.AUTH_USER_MODEL)      
playlist = models.ForeignKey('Playlist', blank=True, null=True)
song = models.ForeignKey('Song')
unblock_flag = models.BooleanField(default=False)

答案的问题是,现在下面的答案中给出的查询出现了错误。

select core_blockedsongs_song.song_id 
from core_blockedsongs join core_blockedsongs_song 
on core_blockedsongs.id=core_blockedsongs_song.blockedsongs_id 

当一个字段从多对多字段更新为外键时,您能建议在查询中需要做哪些更改吗。此处,"歌曲"字段已更新。

您可以在第二个查询中添加伪null列,这样EXCEPT将允许执行该语句,如:

EXCEPT
(select core_blockedsongs_song.song_id, null, null,.... from core_blockedsongs

但我认为这不是你想要的,因为:

EXCEPT返回query1结果中但不在中的所有行查询2 的结果

这意味着要比较整行
我不希望您从第一个查询的结果中排除第二个查询返回的所有song_id
如果是这种情况,则可以使用NOT IN,如下所示:

select songs.id, songs.name, songs.artist, songs.album, songs.albumart, songs."albumartThumbnail", 
cpr.votes, is_requested 
from (
select id, name, artist, album, albumart, "albumartThumbnail" 
from (
select song_id 
from core_plsongassociation 
where playlist_id in (1477)
) as sinpl left join songs 
on sinpl.song_id=id 
where explicit=False
) as songs left join (
select song_id, votes, bool_or(thirdpartyuser_id=177) as is_requested 
from (
select * 
from core_priorityrequests 
where client_id=2876 and is_played=False
) as clpr left join core_priorityrequests_third_party_user 
on clpr.id=priorityrequests_id
group by priorityrequests_id, song_id, votes
) as cpr on songs.id=cpr.song_id 
where songs.id not in (
select core_blockedsongs_song.song_id 
from core_blockedsongs join core_blockedsongs_song 
on core_blockedsongs.id=core_blockedsongs_song.blockedsongs_id 
where core_blockedsongs.unblock_flag = 'f' and core_blockedsongs.client_id=2870
);

或者使用第二个查询的LEFT JOIN,您将从中筛选出匹配的行:

select songs.id, songs.name, songs.artist, songs.album, songs.albumart, songs."albumartThumbnail", 
cpr.votes, is_requested 
from (
select id, name, artist, album, albumart, "albumartThumbnail" 
from (
select song_id 
from core_plsongassociation 
where playlist_id in (1477)
) as sinpl left join songs 
on sinpl.song_id=id 
where explicit=False
) as songs left join (
select song_id, votes, bool_or(thirdpartyuser_id=177) as is_requested 
from (
select * 
from core_priorityrequests 
where client_id=2876 and is_played=False
) as clpr left join core_priorityrequests_third_party_user 
on clpr.id=priorityrequests_id
group by priorityrequests_id, song_id, votes
) as cpr on songs.id=cpr.song_id 
left join (
select core_blockedsongs_song.song_id 
from core_blockedsongs join core_blockedsongs_song 
on core_blockedsongs.id=core_blockedsongs_song.blockedsongs_id 
where core_blockedsongs.unblock_flag = 'f' and core_blockedsongs.client_id=2870
) as c on c.song_id = songs.id
where c.song_id is null;

最新更新