0
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

我在两个不同的数据库中有两个表,它们具有相同的列(侦听器、时间、日期)。我需要总结时间并按听众分组。此查询提供多条记录,我只需要“听众、总时间、收听次数”。我怎样才能用一个查询来做到这一点?

期望的结果;

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

1 回答 1

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
于 2013-02-19T09:42:42.630 回答