select
first_name,
last_name,
c.name as company_name,
sc.`date` as screening_date
from
guests g
inner join
user_guest_group ugs on ugs.guest_id = g.id
inner join
companies c on c.id = g.company_id
inner join
screening_date_guest sdg on sdg.guest_id = g.id
inner join
screening_dates sc on sc.id = sdg.screening_date_id
where
sdg.attending = 1
and
screening_date_id = 1
group by
first_name,
last_name
结果:
Peter, M, Bell Media (ctv), 2015-05-18 00:00:00
Adam, D, Highway Entertainment, 2015-05-18 00:00:00
Todd, F., Multichoice, 2015-05-18 00:00:00
John, D, Talpa, 2015-05-18 00:00:00
Maria, F, UK TV, 2015-05-18 00:00:00
John, L, WBDTD, 2015-05-18 00:00:00
Albert, P, WBDTD, 2015-05-18 00:00:00
我的查询返回结果集。
现在,我希望看到另一个列,每个公司的客人总数。 在这种情况下,我们有2位来自WBTDT的客人,所以应该说total_guest = 2
有人可以帮助我吗?
由于
答案 0 :(得分:1)
这样做的一种方法是在相关的子查询中获取每个公司的计数,所以这可能是你想要的吗?
select
first_name,
last_name,
c.name as company_name,
sc.date as screening_date,
(
select count(*) from guests
inner join
user_guest_group on user_guest_group.guest_id = guests.id
inner join
companies on companies.id = guests.company_id
inner join
screening_date_guest on screening_date_guest.guest_id = guests.id
inner join
screening_dates on screening_dates.id = screening_date_guest.screening_date_id
where
screening_date_guest.attending = 1
and
screening_date_id = 1 and company_id = c.id
) total_guests
from
guests g
inner join
user_guest_group ugs on ugs.guest_id = g.id
inner join
companies c on c.id = g.company_id
inner join
screening_date_guest sdg on sdg.guest_id = g.id
inner join
screening_dates sc on sc.id = sdg.screening_date_id
where
sdg.attending = 1
and
screening_date_id = 1
group by
first_name,
last_name,
c.id,
c.name,
sc.date
答案 1 :(得分:0)
select
first_name,
last_name,
c.name as company_name,
sc.`date` as screening_date,
count(g.id)
from
guests g
inner join
user_guest_group ugs on ugs.guest_id = g.id
inner join
companies c on c.id = g.company_id
inner join
screening_date_guest sdg on sdg.guest_id = g.id
inner join
screening_dates sc on sc.id = sdg.screening_date_id
where
sdg.attending = 1
and
screening_date_id = 1
group by
first_name,
last_name,
c.name