我有兩個表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的一切,我不知道爲什麼
注:這只是一個表的樣本,可能有更多的記錄和用戶inputed日期範圍到可能是不同的,我想他們按月/年
這裏是我的輸出: (其計算所有記錄)
array:1 [▼
0 => {#394 ▼
+"Month": "December"
+"Year": "2016"
+"Mn": "12"
+"tt": "1480568400.000000"
+"pp": "4"
+"aa": "6"
+"cc": "285.00"
}
]
哪項是錯誤的,應該= 2,這裏所需要的輸出我想只爲PROFILE_ID數:
array:1 [▼
0 => {#394 ▼
+"Month": "December"
+"Year": "2016"
+"Mn": "12"
+"tt": "1480568400.000000"
+"pp": "1"
+"aa": "4"
+"cc": "265.00"
}
]
正如你可以看到3個計數是錯誤的,他們假設是: 「頁」: 「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 >>http://sqlfiddle.com/#!9/87dfa8/1
你能幫我解決嗎?
感謝
你能用簡單的話來描述你想要計算,聚合等嗎?僅僅看着查詢就很難理解。 –
3件事要計算:1.如果在start_date之間,則批准的總數2.基於created_date範圍的總計掛起3。總成本只有在狀態獲得批准並且在輸入範圍和所有這些之間的開始日期才需要僅針對輸入的profile_id時,請注意日期範圍可以由給定用戶更改 – user3150060
我想按月和年分組它們,與輸出陣列。現在更清楚了嗎? – user3150060