2012-11-15 102 views
0

我有這個查詢後檢查各種教程應該工作 - 但它沒有。mysql php查詢包含加入計數

$query="SELECT week, year, COUNT(week) AS week_no 
FROM archive_agent_booking 
LEFT JOIN invoice_additions ON invoice_additions.week = archive_agent_booking.week 
WHERE client_id='$account_no' GROUP BY week, year ORDER BY week DESC"; 

的表如下所示:

archive_agent_booking 

+---------+----------+----------+----------+----------+---------+---------+ 
| job_id | week | year | desc | price | date | acc_no | 
+---------+----------+----------+----------+----------+---------+---------+ 


invoice_additions 

+---------+----------+----------+----------+----------+---------+ 
| acc_no | week | year | desc | am_price | am_date | 
+---------+----------+----------+----------+----------+---------+ 

我基本上是想從兩個表中統計每個星期元素並將其顯示爲一個總即便一週值中的一個並不在一個顯示錶格。不知道這是否是最好的解決方案,所以我願意接受替代品。

+0

什麼是你查詢的輸出? – ethrbunny

+0

只是想出了set die()語句。 – Sideshow

回答

1
select 
    week, 
    sum(items) 
from 
    (
     (select week, count(*) as items from archive_agent_booking group by week) 
    union 
     (select week, count(*) from invoice_additions group by week) 
    ) 
group by 
    week 

編輯:我做了你希望看到什麼一些巨大的假設

+0

謝謝:)雖然我沒有時間去測試它,但似乎在表面上工作 - 接下來的半小時...... – Sideshow