English 中文(简体)
MySQL View Problems
原标题:MySQL View Problems

我有以下表格:users,这些表格是自我解释的,answers,其中载有特定用户特定日期的答复清单。

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

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).

提前感谢!

最佳回答

这应当发挥作用,但我建议不使用这一表格,除非回答表总是很小:

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
问题回答
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 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

这确实是骗局,但不是把全国人民军遣返,以换取失踪的答复。

  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




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签