无法弄清楚自我加入查询

时间:2016-09-24 20:09:25

标签: mysql self-join correlated-subquery

我有一个具有以下结构的表。

+-----------------------+--------------+------+-----+---------+-------+
| Field                 | Type         | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+-------+
| linq_order_num        | char(32)     | NO   | PRI | NULL    |       |
| order_status_id       | int(11)      | YES  | MUL | NULL    |       |
| order_id              | varchar(100) | YES  |     | NULL    |       |
| item_name             | varchar(120) | YES  |     | NULL    |       |
| item_cost             | float        | YES  |     | NULL    |       |
| custmer_id            | int(11)      | YES  | MUL | NULL    |       |
| order_date_time       | datetime     | YES  |     | NULL    |       |
| order_category        | varchar(120) | YES  |     | NULL    |       |
| ordered_by            | int(11)      | YES  | MUL | NULL    |       |
| linq_shipping_cost    | float        | YES  |     | NULL    |       |
| website_shipping_cost | float        | YES  |     | NULL    |       |
| total_cost            | float        | YES  |     | NULL    |       |
| advance_amount        | float        | YES  |     | NULL    |       |
| website               | varchar(120) | YES  |     | NULL    |       |
| other                 | varchar(120) | YES  |     | NULL    |       |
| rvn                   | int(11)      | YES  |     | NULL    |       |
| received_date         | datetime     | YES  |     | NULL    |       |
| delivered_date        | datetime     | YES  |     | NULL    |       |
| store_id              | int(11)      | YES  | MUL | NULL    |       |
+-----------------------+--------------+------+-----+---------+-------+

因此,我需要每天查找总订单成本。我可以通过使用此查询来获取它。

select sum(total_cost), date_format(order_date_time,"%Y-%m-%d") from 
       order_item group by date_format(order_date_time,"%Y-%m-%d")

此外,我还需要在交付日期支付剩余的总金额。

select sum(total_cost-advance_amount),date_format(delivered_date,"%Y-%m-%d")
       from order_item group by date_format(delivered_date,"%Y-%m-%d")

并非所有日子都会发生订单而且并非所有交付日都会发生。如果有没有订单的日子,那天的总费用应该显示为零,显示的剩余总金额应该是(total_cost)的总和-advance_amount)当天。

有没有办法可以在一个查询中组合上述两个查询并得到结果?

总结一下特定的日子d: 我需要sum(total_cost)其中ordered_date_time = d, 我需要sum(total_cost -advance_amount),其中deliver_date = d 基本上寻找这样的表:

Date            Total Cost          Total Delivery Amounts
d                 500                     2000
d1                0                       900
d2                900                     0 

我尝试使用子查询。问题是它没有显示d1的情况,其中当天的总成本为0。

查询:

select
    date_format(order_date_time,"%Y-%m-%d") date,
    sum(total_cost) total,
    sum(advance_amount) advance_amount,
    IFNULL( (select sum(total_cost-advance_amount)
from order_item a 
where date_format(a.delivered_date,"%Y-%m-%d") = date_format(d.order_date_time,"%Y-%m-%d") ),0 ) delivery_amount
from order_item d
group by date_format(order_date_time,"%Y-%m-%d"), delivery_amount

1 个答案:

答案 0 :(得分:1)

您可以将两个查询用作派生表,并在日期加入它们。问题是,你需要一个FULL OUTER JOIN,这是MySQL不支持的。因此,您首先需要从两列中提取所有日期

select date(order_date_time) as d from order_item
union
select date(delivered_date)  as d from order_item

对您的查询使用左连接

select
    dates.dt,
    coalesce(tc.total_cost, 0),
    coalesce(tm.total_remaining, 0)
from (
    select date(order_date_time) as dt from order_item
    union
    select date(delivered_date)  as dt from order_item
) dates
left join (
     select sum(total_cost) as total_cost, date(order_date_time) as dt
     from order_item
     group by dt
) tc using(dt)
left join (
     select sum(total_cost-advance_amount) as total_remaining, date(delivered_date)
     from order_item
     group by dt
) tm using(dt)

我还将date_format(..)替换为date(..)。您可以在外部选择或应用程序中格式化日期。