我使用MySql。我有桌子:
邮件
id | user_id | date
1 2 2012/08/21
2 2 2012/08/22
来电
id | user_id | date
1 2 2012/08/21
2 2 2012/08/21
我想在某个日期获得用户的电话和邮件的总和。 e.g:
date | SUM
2012/08/21 3
2012/08/22 1
答案 0 :(得分:3)
SELECT date, COUNT(1) SUM
FROM (SELECT user_id, date FROM mails
UNION ALL
SELECT user_id, date FROM calls) t
GROUP BY date
答案 1 :(得分:2)
假设列id,user_id和date在两个表中具有相同的数据类型:
SELECT date, count(id) as Sum FROM
(
SELECT id, user_id, date from mails
UNION ALL
SELECT id, user_id, date from calls
) total
WHERE user_id = 2
GROUP BY total.date
SQL小提琴here