如何从两个不同的表中获取数据,将其链接到第三个表中的数据,并在单个报告中获取所有内容?
我这里遇到了一点麻烦。我有两个不同的表,一个存储特定横幅每天和每个用户在网站上的印象数,另一个存储在给定日期向任何用户发送的电子邮件数量。
有点像这样:
BANNERS DATE USER_ID CLIENT_ID BANNER_ID IMPRESSIONS ________ _______ _________ ________ ___________ 04/05/11 AX745 CC CC45 4 04/05/11 AX745 KC KC66 1 04/07/11 XY555 DP DP45 2
并且:
EMAILS DATE USER_ID EMAIL_ID CLIENT_ID ________ _______ ________ _________ 04/05/11 AX745 EM001 CC 04/05/11 AX745 EM005 BK 04/07/11 XY555 EM008 DP
除了用户 ID 之外,两个表还包含客户端 ID 和某种形式的日期信息。
我还需要引用客户端 ID 来获取实际客户端的名称,这些名称存储在第三个表中,如下所示:
CLIENTS CLIENT_ID CLIENT _________ ________________________ CC COCA-COLA KC KFC DP DOMINOS BK BURGER KING
我需要一个查询,该查询将在每个用户列表的每个日期返回该用户有多少次展示每个横幅的信息,以及哪个客户拥有该横幅,以及用户在特定日期发送了多少电子邮件以及来自哪个客户。
大致是这样的:
DATE USER_ID BANNER_CLIENT_ID BANNER_ID IMPRESSIONS EMAIL_ID EMAIL_CLIENT_ID ________ _______ ________________ _________ ___________ ________ _______________ 04/05/11 AX745 COCA-COLA CC45 4 EM001 COCA-COLA 04/05/11 AX745 EM005 BURGER-KING 04/05/11 AX745 KFC KC66 1 04/07/11 XY555 DOMINOS DP45 2 EM008 DOMINOS
我可以毫无问题地分别获取每组信息,但到目前为止我还无法将所有数据合并到一个报告中。
任何帮助将不胜感激。
谢谢!
C
I'm having a bit of trouble here. I have two different tables, one that stores the number of impressions a particular banner has had on the website per day and per user, and another the number of e-mails that have been sent any user any given date.
A bit like this:
BANNERS DATE USER_ID CLIENT_ID BANNER_ID IMPRESSIONS ________ _______ _________ ________ ___________ 04/05/11 AX745 CC CC45 4 04/05/11 AX745 KC KC66 1 04/07/11 XY555 DP DP45 2
And:
EMAILS DATE USER_ID EMAIL_ID CLIENT_ID ________ _______ ________ _________ 04/05/11 AX745 EM001 CC 04/05/11 AX745 EM005 BK 04/07/11 XY555 EM008 DP
Both tables contain the client ID, and some form of date information, besides the user id.
I also need to reference the client ID to get the actual client's name, and those are stored in yet a third table, like so:
CLIENTS CLIENT_ID CLIENT _________ ________________________ CC COCA-COLA KC KFC DP DOMINOS BK BURGER KING
I need a query that will return, on a per date per user listing, how many impressions has the user had of each banner, as well as which client owns it, and how many e-mails was the user sent that particular day, and from which client.
Something roughly like so:
DATE USER_ID BANNER_CLIENT_ID BANNER_ID IMPRESSIONS EMAIL_ID EMAIL_CLIENT_ID ________ _______ ________________ _________ ___________ ________ _______________ 04/05/11 AX745 COCA-COLA CC45 4 EM001 COCA-COLA 04/05/11 AX745 EM005 BURGER-KING 04/05/11 AX745 KFC KC66 1 04/07/11 XY555 DOMINOS DP45 2 EM008 DOMINOS
I have no problem getting each set of information separately, but so far I haven't been able to merge all the data into a single report.
Any help will be greatly appreciated.
Thanks!
C
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面的查询应该会为您提供所需的输出。
The query below should give you the desired output.
因为您可能有没有电子邮件的横幅,也可能有没有横幅的电子邮件,因此您需要使用完整外部联接。
包含一些示例数据的查询的完整版本如下所示:
Because you could have either BANNERs without EMAILs and also EMAILS without BANNERS you will need to user a FULL OUTER JOIN.
A full version of the query including some sample data is shown below: