MySQL 查看问题

发布于 2024-12-17 14:29:22 字数 889 浏览 1 评论 0原文

我有以下表格,用户,这是不言自明的,答案,其中包含特定用户在给定日期的响应列表。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

洋洋洒洒 2024-12-24 14:29:22

这应该可行,但我建议不要使用它,除非答案表总是相当小:

select u.id user_id,
       a.created_at,
       max(case when a.user_id = u.id then response end) response
from users u
cross join answers a
group by u.id, a.created_at

This should work, but I recommend against using it unless the answers table will always be fairly small:

select u.id user_id,
       a.created_at,
       max(case when a.user_id = u.id then response end) response
from users u
cross join answers a
group by u.id, a.created_at
等你爱我 2024-12-24 14:29:22
select users.id as user_id, created_at, response from users
  left outer join answers on users.id = answers.user_id
  order by created_at, users.id
select users.id as user_id, created_at, response from users
  left outer join answers on users.id = answers.user_id
  order by created_at, users.id
雨落星ぅ辰 2024-12-24 14:29:22

尝试这个

select t3.user_id, t3.created_at, a.response
from 
(select t2.user_id as user_id, t1.created_at as created_at, null
from
(select distinct created_at
from answers) t1, users t2) t3 answers a
where t3.user_id = a.user_id and t3.created_at = a.created_at

空值,我想左外连接会起作用

select t3.user_id, t3.created_at, a.response
from 
(select t2.user_id as user_id, t1.created_at as created_at, null
from
(select distinct created_at
from answers) t1, users t2) t3 LEFT OUTER JOIN answers a
ON t3.user_id = a.user_id and t3.created_at = a.created_at

try this

select t3.user_id, t3.created_at, a.response
from 
(select t2.user_id as user_id, t1.created_at as created_at, null
from
(select distinct created_at
from answers) t1, users t2) t3 answers a
where t3.user_id = a.user_id and t3.created_at = a.created_at

for nulls, I guess left outer join will work

select t3.user_id, t3.created_at, a.response
from 
(select t2.user_id as user_id, t1.created_at as created_at, null
from
(select distinct created_at
from answers) t1, users t2) t3 LEFT OUTER JOIN answers a
ON t3.user_id = a.user_id and t3.created_at = a.created_at
疾风者 2024-12-24 14:29:22

这可以解决问题,但它不会因缺少响应而返回 NULL,而是返回 0。

  select 
    distinct u.id, a.created_at, MAX(IF(u.id=a.user_id, a.response, 0)) response
  from users u, answers a
    group by id, created_at
    order by created_at, u.id

This does the trick but instead of returning NULL for missing responses, it returns 0.

  select 
    distinct u.id, a.created_at, MAX(IF(u.id=a.user_id, a.response, 0)) response
  from users u, answers a
    group by id, created_at
    order by created_at, u.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文