1

这是我的数据库的外观:

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

感谢您的时间和帮助。

4

2 回答 2

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

输出:

| 身份证 | 姓名 | 日期 | 创建_BY | 当前状态 | UPDATED_BY |
-------------------------------------------------- ---------------------
| 5487 | 乔什 | 2013-05-24 | 1 | 发货 | 瑞克 |
| 5488 | 克伦 | 2013-04-22 | 3 | 包装 | 悉尼 |

这是SQLFiddle演示

于 2013-06-28T00:25:17.413 回答
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
       )
于 2013-06-27T23:40:15.677 回答