无法弄清楚自连接查询
Cant figure out self join query
我有一个具有以下结构的 table。
+-----------------------+--------------+------+-----+---------+-------+
| 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 | |
+-----------------------+--------------+------+-----+---------+-------+
所以每天我需要找到总订单cost.I可以通过使用这个查询得到它。
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")
不是所有的日子,订单都会发生,也不是所有的日子都会交货 happen.If 有一天没有订单,那一天的总成本应该显示为零,显示的总剩余金额应该是当天总计 (total_cost-advance_amount)。
有没有一种方法可以将上述两个查询组合在一个查询中并得到结果?
因此,总结特定日期 d:
我需要 sum(total_cost) 其中 ordered_date_time = d ,
我需要 sum(total_cost -advance_amount) 其中 delivered_date = d
本质上是在寻找这样的 table:
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
您可以将两个查询用作派生表并按日期连接它们。问题是,您需要 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(..)
替换了date_format(..)
。您可以在外部 select 或您的应用程序中格式化日期。
我有一个具有以下结构的 table。
+-----------------------+--------------+------+-----+---------+-------+
| 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 | |
+-----------------------+--------------+------+-----+---------+-------+
所以每天我需要找到总订单cost.I可以通过使用这个查询得到它。
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")
不是所有的日子,订单都会发生,也不是所有的日子都会交货 happen.If 有一天没有订单,那一天的总成本应该显示为零,显示的总剩余金额应该是当天总计 (total_cost-advance_amount)。
有没有一种方法可以将上述两个查询组合在一个查询中并得到结果?
因此,总结特定日期 d: 我需要 sum(total_cost) 其中 ordered_date_time = d , 我需要 sum(total_cost -advance_amount) 其中 delivered_date = d 本质上是在寻找这样的 table:
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
您可以将两个查询用作派生表并按日期连接它们。问题是,您需要 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(..)
替换了date_format(..)
。您可以在外部 select 或您的应用程序中格式化日期。