我有以下SQL查询:
select p.id, p.slug, p.post_author, b.id, b.is_deleted from posts p
left join blocks b
on
((b.blocked_id = p.post_author and blocker_id = <user id>)
or
(b.blocked_id = <user_id> and b.blocker_id = p.post_author))
and b.is_deleted = false
where b.id is null
order by posted_at desc limit 20需要一个相应的反对或声明,我不知道如何把这些括号放在首位。下面是我到目前为止作为修饰语所拥有的内容:
getUnblockedUsersPosts: (builder) => {
builder.leftJoin('blocks as b', function(){
this.on('b.blocked_id', '=', 'posts.post_author')
.andOn('b.blocker_id', '=', parseInt(user.id))
.orOn('b.blocked_id', '=', parseInt(user.id))
.andOn('b.blocker_id', '=', 'posts.post_author')
.andOn('b.is_deleted', knex.raw('false'))
}).where('b.id', 'is', null)
},发布于 2022-01-12 13:02:34
因此,上面的查询是通过以下代码解决的,参考:https://knexjs.org/#Builder-join
getUnblockedUsersPosts: (builder) => {
builder.leftJoin('blocks as b', function(){
this.on(function(){
this.on(function(){
this.on('b.blocked_id', '=', 'posts.post_author')
.andOn('b.blocker_id', '=', parseInt(user.id))
})
this.orOn(function(){
this.on('b.blocker_id', '=', 'posts.post_author')
.andOn('b.blocked_id', '=', parseInt(user.id))
})
})
this.andOn('b.is_deleted', knex.raw('false'))
}).where('b.id', 'is', null)
}https://stackoverflow.com/questions/70680927
复制相似问题