简体   繁体   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

I am stuck with the following requirement and I am finding it difficult to crack the query for it. 我坚持以下要求,我发现很难破解它的查询。

Consider a table customer with the following fields 考虑具有以下字段的表客户

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

Consider another table transaction_history 考虑另一个表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

I need to pick the id from customer table and look up in transaction_history table based on the signup_date and first_payment_date is null . 我需要从customer表中选择id ,并根据signup_datetransaction_history表中signup_datefirst_payment_datenull

Now I need to check if this id is present in transaction_history and check if he has at least 1 entry with product_name = "vod trial" . 现在我需要检查transaction_history中是否存在此id ,并检查他是否至少有1个带有product_name = "vod trial"条目。 If he has then he is a row in the result I want. 如果他有,那么他就是我想要的结果。

At the end I need to calculate the total number of id's from transaction_history who has at least one row where product_name="vod_trial" and this should be on a date basis mentioned in signup_date in customer table. 最后,我需要计算来自transaction_history的id的总数,其中至少有一行product_name="vod_trial" ,这应该是在customer表的signup_date中提到的日期。

I wrote a query in the following manner: 我用以下方式编写了一个查询:

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;

But in the above query I am not able to calculate the total count signup_datewise . 但在上面的查询中,我无法计算signup_datewise的总计数。

Would be great if someone could help me out. 如果有人可以帮助我,那会很棒。

Sample result: 样本结果:

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

I am not sure I fully understand. 我不确定我完全理解。 You want customers without first_payment_date that have a trial entry in the transaction table? 您希望没有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');

Okay, from your last comment it seems, you want customers that have no trial entry in the transaction table, too. 好的,从您上次的评论来看,您也希望客户在交易表中没有试用条目。 And you want to display them with their trial transaction count. 并且您希望使用他们的试用交易计数来显示它们。 So: 所以:

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;

If you even want to group by date, then aggregate: 如果您甚至想按日期分组,那么汇总:

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;

Next try: Join all customers and transactions, such as to only get customers present in the transactions table. 接下来尝试:加入所有客户和交易,例如只获得交易表中的客户。 Then aggregate. 然后聚合。

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;

Or do you want this: 或者你想要这个:

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;

I'd better make this a separate answer. 我最好把它作为一个单独的答案。 You want to find customers that have only one entry in transaction_history and that entry must be 'vod trial'. 您希望找到在transaction_history中只有一个条目且该条目必须为“vod trial”的客户。 So read the transaction table, group by customer id and count. 因此,请阅读交易表,按客户ID和计数分组。 Check your criteria with HAVING. 用HAVING检查你的标准。 Then join the found IDs with the customer table and group by date. 然后将找到的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