簡體   English   中英

如何在單個mysql查詢中執行多個條件?

[英]How to perform multiple condition in single mysql query?

實際上我使用以下查詢在兩個子查詢中執行求和運算

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

如何在單個查詢中執行以使其變得簡單。 請幫助我解決我的問題。

您可以在兩個選擇之間使用聯接

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM