来自两个数据库的mysql sum

时间:2013-02-19 09:23:18

标签: mysql sum

SELECT  db1.table.listener, 
        db2.table.listener, 
        SEC_TO_TIME( SUM( TIME_TO_SEC( db2.table.time ) ) + ( TIME_TO_SEC( db1.table.time ) ) ), 
        count( db2.table.listener )
FROM    table
   INNER JOIN db2.table 
        ON db1.table.listener = db2.table.listener
WHERE db2.table.ldate = '19.02.2013'
   AND db1.table.ldate = '19.02.2013'
GROUP BY db2.table.listener, db1.table.listener

我在两个不同的数据库中有两个表,它们具有相同的列(listener,time,ldate)。我需要总结时间并由听众分组。这个查询给出了多条记录,我只需要“监听器,总时间,监听次数”。如何使用一个查询执行此操作?

期望的结果;

| listeners (from both table, full join) | count of values (from both table) | sum of time (from both table) |

1 个答案:

答案 0 :(得分:0)

见以下内容:

SELECT  db1.table.listener, 
        SEC_TO_TIME( SUM( TIME_TO_SEC( db2.table.time ) ) + 
           ( TIME_TO_SEC( db1.table.time ) ) ), 
        count( db1.table.listener )
FROM    table
   INNER JOIN db2.table 
        ON db1.table.listener = db2.table.listener

WHERE db1.table.ldate = '19.02.2013'

GROUP BY db1.table.listener