gpt4 book ai didi

mysql - 无法弄清楚自连接查询

转载 作者:行者123 更新时间:2023-11-28 23:25:11 25 4
gpt4 key购买 nike

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

+-----------------------+--------------+------+-----+---------+-------+
| 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) where ordered_date_time = d ,我需要 sum(total_cost -advance_amount) where delivered_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

最佳答案

您可以将两个查询用作派生表并按日期加入它们。问题是,您需要一个 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(..)。您可以在外部选择或您的应用程序中格式化日期。

关于mysql - 无法弄清楚自连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39680499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com