0

我有这个查询,在检查各种教程后应该可以工作 - 但它没有。

$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 |
+---------+----------+----------+----------+----------+---------+

我基本上想从两个表中计算每周元素并将它们显示为一个总数,即使其中一个表中没有显示一周值。不知道这是否是最好的解决方案,所以我对替代方案持开放态度。

4

1 回答 1

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

编辑:我对你想看到的东西做了一些巨大的假设

于 2012-11-15T14:37:52.863 回答