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

最新のステータスを取得するには、サブクエリが必要なようです。3 つのテーブルすべてを 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 に答える
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 | 名前 | 日付 | CREATED_BY | 現在のステータス | UPDATED_BY |
-------------------------------------------------- ----------------------
| | 5487 | ジョシュ | 2013-05-24 | 1 | 発送済み | リック |
| | 5488 | クレン | 2013-04-22 | 3 | パッケージ | シドニー | シドニー |

これがSQLFiddleのデモです

于 2013-06-28T00:25:17.413 に答える