使用INNER JOIN的MYSQL计数不起作用

时间:2016-12-30 16:27:03

标签: mysql

我有两张桌子

1)外展

id  profile_id  url
-------------------------
1    2        www.test.com
2    3        www.google.com 
3    4        www.example.com
4    2        www.test2.com
5    2        www.test3.com
6    2        www.test4.com

2)。 outreach_links

id  outreach_id  start_date             created_at        cost    status
-----------------------------------------------------------------------
1    1           2016-12-01 00:00:00  2016-12-07 00:00:00  100.00  Approved
2    1           2016-12-02 00:00:00  2016-12-09 00:00:00  120.00  Approved
3    1           NUll                 2016-12-28 00:00:00  20.00   Pending
4    1           2016-12-05 00:00:00  2016-12-10 00:00:00  35.00   Approved
5    1           2016-12-07 00:00:00  2016-12-13 00:00:00  10.00   Approved
6    2           2016-12-10 00:00:00  2016-12-15 00:00:00  10.00   Pending
7    2           2016-12-13 00:00:00  2016-12-18 00:00:00  10.00   Approved
8    2           2016-12-01 00:00:00  2016-12-28 00:00:00  10.00   Pending
9    2           2016-12-04 00:00:00  2016-12-21 00:00:00  10.00   Approved
10   2           2016-12-09 00:00:00  2016-12-22 00:00:00  15.00   Pending

我正在尝试按月/年进行计数,我认为它有效,但我认为它不起作用因为“profile_id”问题在这里是我的查询:

select monthname(date) as Month,  year(date) as Year,  month(date) as Mn, UNIX_TIMESTAMP(CONCAT(year(date),"-",month(date),"-","01")) as tt,
(select count(*) from outreach_links where year(outreach_links.created_at) = year and month(outreach_links.created_at) = month and status = "Pending" and created_at>="2016-12-01 00:00:00" and created_at<="2016-12-31 00:00:00") as pp,
(select count(*) from outreach_links where year(outreach_links.start_date) = year and month(outreach_links.start_date) = month and status = "Approved" and start_date>="2016-12-01 00:00:00" and start_date<="2016-12-31 00:00:00") as aa,
(select sum(cost) from outreach_links where year(outreach_links.start_date) = year and month(outreach_links.start_date) = month and status = "Approved" and start_date>="2016-12-01 00:00:00" and start_date<="2016-12-31 00:00:00") as cc
from
(select year(outreach_links.created_at) as year, month(outreach_links.created_at) as month, outreach_links.created_at as date 
from outreach_links 
inner join outreach on outreach.id = outreach_links.outreach_id
where outreach_links.created_at>="2016-12-01 00:00:00" and outreach_links.created_at<="2016-12-31 00:00:00" and outreach.profile_id=2
union
select year(outreach_links.start_date) as year, month(outreach_links.start_date) as month, outreach_links.start_date as date
from outreach_links 
inner join outreach on outreach.id = outreach_id
where start_date>="2016-12-01 00:00:00" and start_date<="2016-12-31 00:00:00" and outreach.profile_id=2 ) t1
group by year, month
order by date

所以我的日期范围从“2016-12-01 00:00:00”到“2016-12-31 00:00:00”,这些可以是用户输入的任何日期范围,并尝试根据outreach.profile_id = 2进行计数,我的输出是错误的,它计算所有profile_ids的所有内容,我不知道为什么

注意:这只是表格的一个示例,可能会有更多记录,用户输入的日期范围可能会有所不同,我想按月/年分组

这是我的输出:(它计算所有记录)

array:1 [▼
  0 => {#394 ▼
    +"Month": "December"
    +"Year": "2016"
    +"Mn": "12"
    +"tt": "1480568400.000000"
    +"pp": "4"
    +"aa": "6"
    +"cc": "285.00"
  }
]

哪个错误应该只计算profile_id = 2, 这里是我想要的渴望输出:

array:1 [▼
  0 => {#394 ▼
    +"Month": "December"
    +"Year": "2016"
    +"Mn": "12"
    +"tt": "1480568400.000000"
    +"pp": "1"
    +"aa": "4"
    +"cc": "265.00"
  }
]

正如你所看到的,他们认为3个计数是错误的:     “pp”:“1”     “aa”:“4”     “cc”:“265.00”

这就是我要找的东西:

1). **"pp" is Total Pending** Count when status="Pending" based on created_at
2). **"aa" is Total Approved** Count when status="Approved" based on start_date 
3). **"cc" is Total Cost** Sum of All cost when Status="Approved" and based on start_date
4). Group by Month & Year of the user imputed Date Range

这是一个SQLFIDDLE&gt;&gt; http://sqlfiddle.com/#!9/87dfa8/1

你可以帮我解决一下吗?

由于

1 个答案:

答案 0 :(得分:-1)

我想你想要这样的东西:

 SELECT MONTHNAME(d.date)  AS Month
      , YEAR(d.date)       AS Year
      , MONTH(d.date)      AS Mn
      , SUM(IF(l.status = 'Pending'  AND l.created_at >= d.date AND l.created_at < d.date + INTERVAL 1 MONTH ,1     ,0)) AS pp
      , SUM(IF(l.status = 'Approved' AND l.start_date >= d.date AND l.start_date < d.date + INTERVAL 1 MONTH ,1     ,0)) AS aa
      , SUM(IF(l.status = 'Approved' AND l.start_date >= d.date AND l.start_date < d.date + INTERVAL 1 MONTH ,l.cost,0)) AS cc
   FROM ( SELECT '2016-12-01' + INTERVAL 0 MONTH AS date ) d
   JOIN outreach o
     ON o.profile_id = 2
   LEFT
   JOIN outreach_links l
     ON l.outreach_id = o.id
    AND (  (     l.start_date >= d.date + INTERVAL 0 MONTH
             AND l.start_date <  d.date + INTERVAL 1 MONTH
           )
        OR (     l.created_at >= d.date + INTERVAL 0 MONTH
             AND l.created_at <  d.date + INTERVAL 1 MONTH
           )
        )
  GROUP BY d.date

修改

要指定范围的结束日期以及开始日期,内联视图d可以返回两个日期值。外部查询可以引用第二个日期值,在此示例中为d.end_date,以及范围d.date的开头。

 SELECT MONTHNAME(d.date)  AS Month
      , YEAR(d.date)       AS Year
      , MONTH(d.date)      AS Mn
      , SUM(IF(l.status = 'Pending'  AND l.created_at >= d.date AND l.created_at < d.end_date,1     ,0)) AS pp
      , SUM(IF(l.status = 'Approved' AND l.start_date >= d.date AND l.start_date < d.end_date,1     ,0)) AS aa
      , SUM(IF(l.status = 'Approved' AND l.start_date >= d.date AND l.start_date < d.end_date,l.cost,0)) AS cc
   FROM ( SELECT '2016-12-01' + INTERVAL 0 MONTH AS date
               , '2016-12-16' + INTERVAL 0 MONTH AS end_date
         ) d
   JOIN outreach o
     ON o.profile_id = 2
   LEFT
   JOIN outreach_links l
     ON l.outreach_id = o.id
    AND (  (     l.start_date >= d.date
             AND l.start_date <  d.end_date
           )
        OR (     l.created_at >= d.date
             AND l.created_at <  d.end_date
           )
        )
  GROUP BY d.date, d.end_date