欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql 查询 两个表中不同字段的 和,并通过两个表的时间来分组

程序员文章站 2022-07-01 14:56:24
大的方面 为 select ... from (查询表) a where ... group by .... order by ..... 查询表 为 将两个表union , union时, 两张表查询出来的字段名 必须是一致的, 没有的字段, 可以用 “ 0 as column ” 来补充。 1 ......

 

 

 

 

 

( select
sum( a.cost_sum ) as sum_cost,
sum( a.phone_sum ) as sum_phone,
sum( a.arrive_sum ) as sum_arrive,
format( sum( a.cost_sum ) / sum( a.arrive_sum ), 0 ) as arrive_cb,
a.time as time 
from
    (
        select
            from_unixtime( time, "%y-%m-%d" ) as time,
            sum( cost ) as cost_sum,
            sum( arrive_true ) as arrive_sum,
            sum( phone ) as phone_sum 
        from
            `cmf_test_jj_data` 
        group by
            `time` union
            (
        select
            from_unixtime( time, "%y-%m-%d" ) as time,
            0 as cost_sum,
            sum( arrive_num ) as arrive_sum,
            0 as phone_sum 
        from
            cmf_test_jj_arrive_new 
        group by
            time 
            ) 
    ) a 
where
    ( `a`.`time` >= 0 and `a`.`time` <= 1551801600 ) 
group by
    a.time 
order by
    a.time desc 
    )

 

大的方面   为   select  ... from  (查询表) a where  ... group by .... order by .....

 

查询表  为  将两个表union ,   union时, 两张表查询出来的字段名 必须是一致的,  没有的字段, 可以用   “ 0 as column ”   来补充。

 

 

 

 

 

 

 

 

 

 

1