实际上我使用以下查询在两个子查询中执行求和操作
select date,IFNULL(SUM(total_amount),0) as total from sales
where date='".date("d/m/Y")."'
select date,IFNULL(SUM(total_amount),0) as total from sales
where str_to_date(date,'%d/%m/%Y') >= '".date("Y-m-01")."' and
str_to_date(date,'%d/%m/%Y') <= '".date("Y-m-t")."'
如何在单个查询中执行以简化操作。请帮我解决我的问题。
答案 0 :(得分:0)
您可以在两个选择
之间使用联接select t1.date, t1.total, t2.total
from (
select date,IFNULL(SUM(total_amount),0) as total from sales
where date='".date("d/m/Y")."'
) t1
left join (
select date,IFNULL(SUM(total_amount),0) as total from sales
where str_to_date(date,'%d/%m/%Y') >= '".date("Y-m-01")."' and
str_to_date(date,'%d/%m/%Y') <= '".date("Y-m-t")."'
) y2 on t1.date = t2.date