1

http://sqlfiddle.com/#!2/b814c/6/0

select listener, SEC_TO_TIME( SUM( TIME_TO_SEC(time))) as total_time, count(listener) as total_listened from db1 where date = "2013-02-20" group by listener

如何将 db2 值添加到此查询?

我尝试了嵌套查询,但它运行得太慢并且不只显示 db2 侦听器。

如果它改变了某些东西,这两个表都在不同的数据库中。

结果应该与此样本类似;

LISTENER    TOTAL_TIME  TOTAL_LISTENED
listener1   00:15:39    1
listener2   00:22:59    2
listener3   00:13:34    1
4

2 回答 2

1

这是您的原始查询

select listener, SEC_TO_TIME( SUM( TIME_TO_SEC(time))) as total_time,
count(listener) as total_listened from db1 where date = "2013-02-20"
group by listener

尝试结合listenertime

然后,将您的操作应用到他们的 UNION

SET @GivenDate='2013-02-20';
SELECT listener,SEC_TO_TIME( SUM( TIME_TO_SEC(time))) as total_time,
COUNT(listener) as total_listened
FROM
(
    SELECT listener,time FROM db1
    where date = @GivenDate
    UNION ALL
    SELECT listener,time FROM db2
    where date = @GivenDate
) A GROUP BY listener;
于 2013-02-19T22:45:26.923 回答
1

你在找吗?

select listener, SEC_TO_TIME( SUM( TIME_TO_SEC(time))) as total_time, count(listener) as total_listened from 
(
  SELECT * FROM db1
  UNION ALL
  SELECT * FROM db2
) A
where date = "2013-02-20" group by listener

SQL 小提琴演示

于 2013-02-19T22:46:27.017 回答