MySQL 查看问题
我有以下表格,用户,这是不言自明的,答案,其中包含特定用户在给定日期的响应列表。
users
-----
ID FIRST_NAME LAST_NAME
1 Joe Bloggs
2 Fred Sexy
3 Jo Fine
4 Yo Dude
5 Hi There
answers
-------
ID CREATED_AT RESPONSE USER_ID
1 2011-01-01 3 1
2 2011-01-01 4 2
3 2011-01-02 5 5
我的目标是构建一个输出以下内容的视图:
USER_ID CREATED_AT RESPONSE
1 2011-01-01 3
2 2011-01-01 4
3 2011-01-01 NULL
4 2011-01-01 NULL
5 2011-01-01 NULL
1 2011-01-02 NULL
2 2011-01-02 NULL
3 2011-01-02 NULL
4 2011-01-02 NULL
5 2011-01-02 5
我一直在尝试在一个 SELECT 语句中执行此操作,但我不相信这是可能的,也许我遗漏了一些东西?我可以使用多个语句完成输出,但我正在寻找一种更优雅的方法,它可以位于一个视图(或多个视图)中。
提前致谢!
I have the following tables, users which is self explanatory and answers which contains a list of responses on a given date for a specific user.
users
-----
ID FIRST_NAME LAST_NAME
1 Joe Bloggs
2 Fred Sexy
3 Jo Fine
4 Yo Dude
5 Hi There
answers
-------
ID CREATED_AT RESPONSE USER_ID
1 2011-01-01 3 1
2 2011-01-01 4 2
3 2011-01-02 5 5
My aim is to build a view which would output the following:
USER_ID CREATED_AT RESPONSE
1 2011-01-01 3
2 2011-01-01 4
3 2011-01-01 NULL
4 2011-01-01 NULL
5 2011-01-01 NULL
1 2011-01-02 NULL
2 2011-01-02 NULL
3 2011-01-02 NULL
4 2011-01-02 NULL
5 2011-01-02 5
I have been trying to do this in one SELECT statement but I don't believe it is possible, maybe I'm missing something? I can accomplish the output with multiple statements but I'm looking for a more elegant method which can sit in a view (or multiple views).
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该可行,但我建议不要使用它,除非答案表总是相当小:
This should work, but I recommend against using it unless the answers table will always be fairly small:
尝试这个
空值,我想左外连接会起作用
try this
for nulls, I guess left outer join will work
这可以解决问题,但它不会因缺少响应而返回 NULL,而是返回 0。
This does the trick but instead of returning NULL for missing responses, it returns 0.