以下是样本Db结构
我希望结果在bookshelfjs中,以便
1)广告系列表
中的所有列2)一列将具有实例数量的列属于广告系列
3)一列将具有实例数量的列属于一个广告系列,并且status_id = 3
我知道原始查询:
form_for(@user, url: admin_resource_path(@user)
我使用
尝试了相同的查询 select campaign.*,
(select count(*) from instance where instance.campaign_id=campaign.id) as totalInstances,
(select count(*) from instance where instance.campaign_id=campaign.id and instance.status.id=3) as duplicateInstances
from campaign
但没有运气。我怎样才能做到这一点?
答案 0 :(得分:0)
knex('campaign').select([
'campaign.*',
(builder) => builder
.count('*')
.from('instance')
.where('instance.campaign_id', knex.raw('??', ['campaign.id']))
.as('totalInstances'),
(builder) => builder
.count('*')
.from('instance')
.where('instance.campaign_id', knex.raw('??', ['campaign.id']))
.where('instance.status.id', 3)
.as('duplicateInstances')
]).toSQL()
select
"campaign".*,
(select count(*) from "instance" where "instance"."campaign_id" = "campaign"."id") as "totalInstances",
(select count(*) from "instance" where "instance"."campaign_id" = "campaign"."id" and "instance"."status"."id" = ?) as "duplicateInstances"
from "campaign"