【发布时间】:2018-01-18 22:53:14
【问题描述】:
我有两个使用联合组合的 mysql 查询。我得到了我想要的结果。但是,我想知道是否有一种方法可以组合具有相同名称的字段。例如,我从图像中的第一个表中获取结果。我想要做的是将两个名为“test”的促销字段合并到一个字段中,并添加三列(免费、无、付费)。我想要的结果在第二个表中。
这是我的查询
select users.refer as promo,
count( case when plan = 'free' then 1 end ) as free,
count( case when plan = '' then 1 end ) as none,
count( case when plan <> 'free' and plan <> '' then 1 end) as pay,
promos.status as status
from users
inner join promos on users.refer = promos.name
where refer <> ''
group by refer
Union
select subscriptions.promo as promo,
count( case when plan = 'free' then 1 end ) as free,
count( case when plan = '' then 1 end ) as none,
count( case when plan <> 'free' and plan <> '' then 1 end) as pay,
promos.status as status
from subscriptions
inner join promos on subscriptions.promo = promos.name
inner join users on subscriptions.user_id = users.id
where promo <> ''
group by promo
【问题讨论】:
-
如果两个查询中的
status不同,最终的值应该是多少?