如何在访问2013中将2个表计入另一个表?

时间:2017-06-14 09:32:06

标签: ms-access

我在访问2013中有2个系统日志表,一个是通过电子邮件发送的,另一个是由userid发送的。 LOG1

  

电子邮件datetime msg

的log 2

  

userid datetime msg

我想计算用户每月发送多少消息并保存到表user_msg_count

  

yearmonth userid msg_count

SELECT format([log1].datetime,"yyyy/mm") AS yearmonth, log1.email, COUNT(log1.msg) AS msg_count INTO user_msg_count
FROM log1
GROUP BY FORMAT(log1.datetime,"yyyy/mm", log1.email;

然后我从users表中获取userid

用户

  

用户ID电子邮件

UPDATE user_msg_count, users SET user_msg_count.userid = users.userid
WHERE user_msg_count.email = users.email;

但我不知道如何将log2加到user_msg_count表中。

1 个答案:

答案 0 :(得分:0)

电子邮件是发件人(用户)电子邮件还是收件人?

如果是用户电子邮件:

LogUnion查询:

SELECT Users.UserID, Format([DateSend],"yyyymm") AS YrMo, "L1" AS Source FROM Users INNER JOIN Log1 ON Users.emailAdd = Log1.email
UNION ALL SELECT UserID, Format([DateSend],"yyyymm"), "L2" FROM Log2;

TotalCount查询:

SELECT LogUnion.UserID, LogUnion.YrMo, Count(*) AS CountMsg
FROM LogUnion
GROUP BY LogUnion.UserID, LogUnion.YrMo;

嵌套的一体化查询:

SELECT LogUnion.UserID, LogUnion.YrMo, Count(*) AS CountOfSource
FROM (SELECT Users.UserID, Format([DateSend],"yyyymm") AS YrMo, "L1" AS Source FROM Users INNER JOIN Log1 ON Users.emailAdd = Log1.email
UNION ALL SELECT UserID, Format([DateSend],"yyyymm"), "L2" FROM Log2) AS LogUnion
GROUP BY LogUnion.UserID, LogUnion.YrMo;