需要sql查询最近的状态

时间:2013-06-27 23:21:04

标签: mysql

以下是我的数据库的外观:

||  order  ||  ||  status  ||   ||  employees ||
==========     ==============   ===============
id             id               id
name           current_status   display_name
created_by     date(timestamp)  
date           updated_by

我想要的是每个订单,订单详情和最新状态:

order.id // == status.id
order.name 
order.date
order.created_by // = employees.id
status.current_status //latest status MAX(date)
employees.display_name //status.upated_by = employees.id

以下是一些示例数据:

==================================================================
||    order                                     ||
==================================================================
|| id      ||     ||  name   ||   ||  created_by ||  ||  date   ||
==================================================================
5487       ||     ||  Josh   ||   ||  1         ||  ||  2013-24-05
5488       ||     ||  Kren   ||   ||  3         ||  ||  2013-22-04

====================================================================
||          status                                               ||
===================================================================
||   id    ||     ||current_status ||  || date     || || updated_by
===================================================================
||  5487   ||     || Packaged      ||  || 2013-24-05 22-09 || 2
||  5488   ||     || Packaged      ||  || 2013-25-05 12-05 || 3
||  5487   ||     || Shipped       ||  || 2013-28-05 16-05 || 1

===================================================================
||             employees                                        ||
==================================================================
||       id       ||    ||      display_name                ||
===================================================================
|| 1              ||    ||   Rick                               ||
|| 2              ||    ||   Dave                               ||
|| 3              ||    ||   Sydney                             ||

这是我尝试的但是current_status不对:

SELECT a.id, a.name, a.created_by, a.date, b.current_status, c.display_name 
FROM id a INNER JOIN ( SELECT id, current_status, MAX(date) FROM status GROUP BY id) b 
ON b.id=a.id INNER JOIN users c ON b.updated_by=c.id AND a.created_by = c.id 
GROUP BY a.id

感谢您的时间和帮助。

2 个答案:

答案 0 :(得分:0)

听起来您需要进行子查询才能获得最新状态。我相信用where子句加入所有三个表只包括max(date)就可以了。

SELECT * FROM orders o 
  JOIN status st ON o.id=st.id 
  JOIN employees em ON o.created_by = em.id 
 WHERE st.date = (
       SELECT max(date) 
         FROM status 
        WHERE id=st.id 
        GROUP BY id
       )

答案 1 :(得分:0)

没有相关子查询的另一种方法

SELECT o.id, o.name, o.date, o.created_by, z.current_status, e.display_name updated_by
  FROM orders o LEFT JOIN
(
    SELECT s.id, s.current_status, s.updated_by
      FROM status s JOIN
    (
        SELECT id, MAX(date) date
          FROM status
         GROUP BY id
    ) q ON s.id = q.id AND s.date = q.date
) z ON o.id = z.id JOIN employees e 
    ON z.updated_by = e.id

输出:

|   ID | NAME |       DATE | CREATED_BY | CURRENT_STATUS | UPDATED_BY |
-----------------------------------------------------------------------
| 5487 | Josh | 2013-05-24 |          1 |        Shipped |       Rick |
| 5488 | Kren | 2013-04-22 |          3 |       Packaged |     Sydney |

这是 SQLFiddle 演示