加入两个表和总和计数问题

时间:2016-04-21 05:43:17

标签: php mysql jointable

我有三张桌子

表1

p_id       p_name                         
75         test1             
76         test2             
77         test3             
78         test4             
79         test5             
...

表2

id        c_id                         
1         1             
2         1             
3         3             
4         4             
5         4             

表3

period        p_id    p_name   total_count  active_count cance_count no_of_occur          
2016-04-13    79      test5    2            2            0           2
2016-04-18    76      test2    2            2            0           2
2016-04-20    79      test5    1            1            0           1
...

我需要结果:

SELECT 
   DATE(DATE_ADD(created_at, INTERVAL 19800 SECOND)) AS period, table1.p_id, 
   MIN(mdcpev.value) AS product_name,
   COUNT(table1.c_id) AS total_count,
   SUM(status = 1) AS active_count,
   SUM(status = 2) AS cancel_count,
   COUNT(table3.id) AS no_of_occur,
     FROM table1
         INNER JOIN table2 AS mdcpev ON mdcpev.p_id = p_id
         LEFT JOIN table3 AS mdspo ON mdspo.c_id = tabe1.c_id
         GROUP BY DATE(DATE_ADD(created_at, INTERVAL 19800 SECOND)),p_id

对于我写过的代码,但它给了我错误的计数

   period        p_id    p_name   total_count  active_count cance_count no_of_occur          
    2016-04-13    79      test5    3            3            0           3
    2016-04-18    76      test2    3            3            0           2
    2016-04-20    79      test5    1            1            0           1

修改

我当前的查询结果是

active_count

这里我得到no_of_occur 3这是错误的应该是2而String url = "https://www.fbbexample.com/abc/"+ URLEncoder.encode(fbid,"UTF-8"); 应该是2并且当前显示3 任何人都可以指导我查询此查询中的错误吗?

谢谢

3 个答案:

答案 0 :(得分:1)

导致区别的是与table3的连接,因为在c_id列中有1和4两次,这些将复制记录。

我会按如下方式更改计数:

COUNT(distinct table1.c_id) AS total_count,
Count(distinct case when status = 1 then table1.c_id else null end) AS active_count,
Count(distinct case when status = 2 then table1.c_id else null end) AS cancel_count,
COUNT(distinct table3.id) AS no_of_occur,

计数内的不同确保只计算不同的值。但是,no_of_occur仍然存在问题。如果它基于table3.id,那么在第一条记录中你仍然会得到3为p_id 79,因为table1中的第一条记录与table3中的2条记录相关,而table1中的第二条记录与table3中的第三条记录相关。因此,我认为您的期望要么不正确,要么您的no_of_occur计算根本不应该与table3相关联。但在这种情况下,您甚至不需要在查询中使用table3。这只是你可以回答的问题。

答案 1 :(得分:1)

SUM(status = 1) AS active_count,对于所有条件,它总是返回true(1)。所以输错了。

我希望这个查询能够正常运行。请检查。

SELECT
    DATE_FORMAT(table1.created_at,'%Y-%m-%d'),
    table1.p_id,
    table2.p_name,
    COUNT(table1.c_id) AS total_count,
    SUM(CASE WHEN status= 1 THEN 1  ELSE 0 END) AS active_count,
    SUM(CASE WHEN status= 2 THEN 1  ELSE 0 END) AS cancel_count,
    COUNT(table3.id) AS no_of_occur
       FROM table1
       JOIN table2
       on table2.p_id=table1.p_id
       JOIN table3
       ON table3.c_id=table1.c_id
       Group by DATE_FORMAT(table1.created_at,'%Y-%m-%d'),table1.p_id

答案 2 :(得分:1)

尝试此查询,它会起作用。

    SELECT
        DATE_FORMAT(table1.created_at,'%Y-%m-%d'),
        table1.p_id,
        table2.p_name,
        COUNT(table1.c_id) AS total_count,
        COUNT(CASE WHEN status = 1 THEN table1.c_id ELSE NULL end) AS active_count,
        COUNT(CASE WHEN status = 2 THEN table1.c_id ELSE NULL end) AS cancel_count,
        COUNT(table3.id) AS no_of_occur
           FROM table1
           JOIN table2
           on table2.p_id=table1.p_id
           JOIN table3
           ON table3.c_id=table1.c_id
           Group by DATE_FORMAT(table1.created_at,'%Y-%m-%d'),table1.p_id

<强>输出: 我从问题中获取了dumy数据并在我的系统中进行了尝试..

enter image description here