简体   繁体   中英

getting latest data in one to many relation table

i would like to fetch data from table a,b,c but order by most recent data of table response

table casework has this structure ( simplified):

casework_id | problem   | user_id          
------------+-----------+-------
1           | Problem1  | 1  
2           | Problem2  | 2
3           | Problem3  | 1
4           | Problem4  | 3

table user has this structure ( simplified):

user_id | name         
--------+-----------------
1       | peter  
2       | Sam  
3       | Tom  
4       | Steve

table response has this structure ( simplified):

response_id | response   | casework_id | created           
------------+-----------+--------------+-------
1           | responce1  |  1      | 2012-10-14 11:28:31
2           | responce2  |  1      | 2012-9-10 11:28:31 
3           | responce3  |  1      | 2012-9-2 11:28:31
4           | responce4  |  3      | 2012-8-3 11:28:31
4           | responce5  |  3      | 2012-8-2 11:28:31

I am looking the query to fetch data order by latest responce and group by casework_id

I. e. required out put is 

casework_id | problem   | name  | responce  | created        
------------+-----------+-------+-----------+---------
1           | Problem1  | peter | responce1 | 2012-10-14 11:28:31
2           | Problem2  | Sam   | Null      | Null
3           | Problem3  | peter | responce4 | 2012-8-3 11:28:31 
4           | Problem4  | Tom   | Null      | Null

I would be most grateful if one of you kind people could point me in the right direction.

You can use the following:

select c.casework_id,
  c.problem,
  u.name,
  r2.response,
  r1.created
from casework c
left join user u
  on c.user_id = u.user_id
left join
(
  select max(created) created, casework_id
  from response r
  group by casework_id
) r1
  on c.casework_id = r1.casework_id
left join response r2
  on r1.created = r2.created
  and r1.casework_id = r2.casework_id

See SQL Fiddle with Demo

If you want to include both the user that created the casework and then who responsed, then you will want to join on the user table twice:

select c.casework_id,
  c.problem,
  u1.name CreatedByName,
  r2.response,
  r1.created,
  u2.name ReponseName
from casework c
left join user u1
  on c.user_id = u1.user_id
left join
(
  select max(created) created, casework_id
  from response r
  group by casework_id
) r1
  on c.casework_id = r1.casework_id
left join response r2
  on r1.created = r2.created
  and r1.casework_id = r2.casework_id
left join user u2
  on r2.user_id = u2.user_id

See SQL Fiddle with demo

I have not tested it, but it might give you an idea

select c.casework_id, c.problem, 
(select name from user u where u.user_id = c.user_id ), 
(select r.reponse from response r where r.casework_id = c.casework_id ORDER BY r.created DESC LIMIT 1),
(select r.created from response r where r.casework_id = c.casework_id ORDER BY r.created DESC LIMIT 1),
from casework c
SELECT responce.casework_id, problem, name, responce, created 
FROM responce
JOIN
   (SELECT casework_id, problem, name 
    FROM casework JOIN user 
    ON casework.userid=user.userid) AS A
ON responce.casework_id=A.casework_id
ORDER BY responce, responce.casework_id

尝试这个

select c.caseword_id, c.problem, u.name,  response.response, responce.created from  asework c inner join user u on u.user_id = c.user_id left outer join select casework_id from response having max(created) group by casework_id) responsedata on responsedata.casework_id = c.casework_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM