数据库
首页 > 数据库> > mysql 查询 两个表中不同字段的 和,并通过两个表的时间来分组

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

作者:互联网

 

 

 

 

 

( 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

 

标签:mysql,sum,表中,phone,字段,cost,time,arrive,SELECT
来源: https://www.cnblogs.com/Skate0rDie/p/10487336.html