zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

mysql中将多行数据合并成一行显示

mysql数据 显示 合并 一行 多行 中将
2023-09-27 14:22:13 时间
select '1' as type ,sum(firm_order_num) as firm_order_num ,sum(order_total_amount) as order_total_amount  from daily_order_report_income as a  where order_date='2020-02-24' and ad_code='110100'

union all 

select '2' as type ,sum(firm_order_num) as firm_order_num ,sum(order_total_amount) as order_total_amount  from daily_order_report_income as a  where order_date='2020-02-25' and ad_code='110100'


select 
MAX(CASE type WHEN '1' THEN a.firm_order_num ELSE 0 END) AS last_firm_order_num, 
MAX(CASE type WHEN '2' THEN a.firm_order_num ELSE 0 END) AS ben_firm_order_num, 
MAX(CASE type WHEN '1' THEN a.order_total_amount ELSE 0 END) AS last_order_total_amount, 
MAX(CASE type WHEN '2' THEN a.order_total_amount ELSE 0 END) AS ben_order_total_amount
from (
select '1' as type ,sum(firm_order_num) as firm_order_num ,sum(order_total_amount) as order_total_amount  from daily_order_report_income as a  where order_date='2020-02-24' and ad_code='110100'

union all 

select '2' as type ,sum(firm_order_num) as firm_order_num ,sum(order_total_amount) as order_total_amount  from daily_order_report_income as a  where order_date='2020-02-25' and ad_code='110100'

)as  a