如何在单个mysql查询中执行多个条件?

时间:2016-12-13 06:34:51

标签: php mysql

实际上我使用以下查询在两个子查询中执行求和操作

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")."'

如何在单个查询中执行以简化操作。请帮我解决我的问题。

1 个答案:

答案 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