繁体   English   中英

MYSQL:如何使用Inner join连接两个表,然后计算以下示例中第二个表的总数

[英]MYSQL: How to join two tables using Inner join and then calculatin the total number from the second table for the following examples

我坚持以下要求,我发现很难破解它的查询。

考虑具有以下字段的表客户

id   signup_date  first_payment_date 
10    2015-03-20     null
11    2015-03-20     null
12    2015-03-20     null
13    2015-03-20     null
14    2015-05-23     null
15    2015-05-23     null

考虑另一个表transaction_history

id   product_name
10    vod trial
10    vod trial 
11    vod trial
12    vod trial
12    vod
13    vod trial
14    vod trial
15    vod trial
15    vod trial

我需要从customer表中选择id ,并根据signup_datetransaction_history表中signup_datefirst_payment_datenull

现在我需要检查transaction_history中是否存在此id ,并检查他是否至少有1个带有product_name = "vod trial"条目。 如果他有,那么他就是我想要的结果。

最后,我需要计算来自transaction_history的id的总数,其中至少有一行product_name="vod_trial" ,这应该是在customer表的signup_date中提到的日期。

我用以下方式编写了一个查询:

SELECT 
    ts.guid,
    cs.signup_date,
    (SELECT 
        COUNT(ts2.guid)
     FROM
        transaction_history ts2
     WHERE
        cs.guid = ts2.guid
        AND ts2.product_name = "vod trial"
    HAVING COUNT(ts2.guid) = 1) AS count_ts_guid
FROM
    customer AS cs,
    transaction_history AS ts
WHERE
    cs.guid = ts.guid
    AND cs.first_payment_date IS NULL;

但在上面的查询中,我无法计算signup_datewise的总计数。

如果有人可以帮助我,那会很棒。

样本结果:

date         new trials
2015-03-20      2
2015-05-23      1

我不确定我完全理解。 您希望没有first_payment_date的客户在交易表中有试用条目吗?

select *
from customer
where first_payment_date is null
and id in (select id from transaction_history where product_name = 'vod trial');

好的,从您上次的评论来看,您也希望客户在交易表中没有试用条目。 并且您希望使用他们的试用交易计数来显示它们。 所以:

select signup_date, 
  (
    select count(*) 
    from transaction_history th
    where th.product_name = 'vod trial'
    and th.id = c.id
  )
from customer c
where first_payment_date is null;

如果您甚至想按日期分组,那么汇总:

select signup_date, 
  sum((
    select count(*) 
    from transaction_history th
    where th.product_name = 'vod trial'
    and th.id = c.id
  ))
from customer c
where first_payment_date is null
group by signup_date;

接下来尝试:加入所有客户和交易,例如只获得交易表中的客户。 然后聚合。

select c.signup_date, count(*) 
from customer c
join transaction_history th on th.id = c.id and th.product_name = 'vod trial'
where c.first_payment_date is null
group by c.signup_date;

或者你想要这个:

select c.signup_date, count(case when th.product_name = 'vod trial' then 1 end) 
from customer c
join transaction_history th on th.id = c.id
where c.first_payment_date is null
group by c.signup_date;

我最好把它作为一个单独的答案。 您希望找到在transaction_history中只有一个条目且该条目必须为“vod trial”的客户。 因此,请阅读交易表,按客户ID和计数分组。 用HAVING检查你的标准。 然后将找到的ID与客户表和按日期分组。

select c.signup_date, count(*)
from customer c
join
(
  select id
  from transaction_history
  group by id
  having count(*) = 1
  and min(product_name) = 'vod trial'
) t on t.id = c.id
group by c.signup_date;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM