簡體   English   中英

MySQL查詢-加入子查詢

[英]MySQL Query - Join Subquery

我有的

我有多個這樣的表:

  • tbl_hw_inventar(hw_id,主機名,hw_typ_idfs,hw_created_user_idfs等)
  • tbl_hw_typ(hw_typ_id,hw_typ_title等)
  • tbl_user(用戶ID,用戶名等)
  • tbl_hw_edited(hw_edited_id,hw_edited_client_idfs,hw_edited_date,hw_edited_user_idfs)

我需要的

我要輸出包含以下信息的表:

 hw_id | Hostname | created                         | last edited 

 12315 | client-01 | 2015-05-06 15:31:06 (username) | 2015-07-02 09:46:17 (username) |

問題

如您所見,我可以獲得帶有外鍵和對“ tbl_hw_typ ”的內部 tbl_hw_typ之類的信息。 對於帶有“ hw_created_user_idfs ”和內部hw_created_user_idfs以獲取用戶 ID 用戶名的信息也是如此。

但是,如何獲取最后編輯的 日期時間用戶名

在我的表“ tbl_hw_edited ”中,我有這樣的條目:

row id | hw_id | datetime | user_id

編碼

到目前為止,我的SQL查詢如下所示:

SELECT `tbl_hw_inventar`.*, `tbl_hw_typ`.`hw_typ_title`, `tbl_user`.`username`, `tbl_hw_edited`.`hw_edited_id` 
FROM `tbl_hw_inventar` 
INNER JOIN `tbl_hw_typ` 
ON `tbl_hw_inventar`.`hw_typ_idfs` = `tbl_hw_typ`.`hw_typ_id` 
INNER JOIN `tbl_user` 
on `tbl_hw_inventar`.`hw_create_user_idfs` = `tbl_user`.`id`
JOIN (
    SELECT MAX(`tbl_hw_edited`.`hw_edited_id`), `tbl_hw_edited`.`hw_edited_client_idfs`
    FROM `tbl_hw_edited`
    ) `tbl_hw_edited` ON `tbl_hw_inventar`.`hw_id` = `tbl_hw_edited`.`hw_edited_client_idfs`
ORDER BY `tbl_hw_inventar`.`hw_id` ASC

那么如何導出信息? 看來我必須在查詢中進行子查詢。 但是我每次嘗試都失敗了。

謝謝你的幫助

編輯

按照提議,我將為每個表提供更多信息(表數據):

-tbl_hw_inventar-
| hw_id | hw_hostname | hw_create_date      | hw_create_user_idfs |
| 1     | client-01   | 2015-03-06 11:57:42 | 1                   |
| 2     | client-02   | 2015-09-21 21:17:00 | 3                   |

-tbl_hw_edited-
| hw_edited_id | hw_edited_client_idfs | hw_edited_date      | hw_edited_user_idfs |
| 1            | 1                     | 2015-09-24 17:30:22 | 1                   |
| 2            | 2                     | 2015-09-24 16:33:22 | 2                   |
| 3            | 1                     | 2015-09-24 23:30:22 | 2                   |
| 4            | 2                     | 2015-09-24 20:30:22 | 3                   |

-tbl_user-
| id | username |
| 1  | ismaelw  |
| 2  | skalb    |
| 3  | yrumpel  |

因此,作為最終結果,我需要這樣的輸出:

| hw_id | hostname  | created                       | edited                      |
| 1     | client-01 | 2015-03-06 11:57:42 (ismaelw) | 2015-09-24 23:30:22 (skalb) |

如果我正確地解釋了您的表,則需要為每個hw_edited_client_idfs(與hw_id連接)找到最大(編輯日期)

                            SELECT
                                  hw_edited_client_idfs
                                , MAX(hw_edited_date) AS last_edit_dt
                            FROM tbl_hw_edited
                            GROUP BY hw_edited_client_idfs

通過該結果,您可以將其重新連接到源表,以發現與最大(編輯日期)關聯的用戶

                SELECT
                      het.*
                FROM tbl_hw_edited AS het
                      INNER JOIN (
                            SELECT
                                  hw_edited_client_idfs
                                , MAX(hw_edited_date) AS last_edit_dt
                            FROM tbl_hw_edited
                            GROUP BY hw_edited_client_idfs
                      ) AS mx ON het.hw_edited_client_idfs = mx.hw_edited_client_idfs
                                  AND het.hw_edited_date = mx.last_edit_dt

然后,該結果使用如下:

SELECT
      i.hw_id
    , i.hw_hostname
    , uc.username created_by
    , he.hw_edited_date last_edit_date
    , ue.username last_edit_by
FROM tbl_hw_inventar AS i
      INNER JOIN tbl_user AS uc ON i.hw_create_user_idfs = uc.id
      LEFT OUTER JOIN (
                SELECT
                      het.*
                FROM tbl_hw_edited AS het
                      INNER JOIN (
                            SELECT
                                  hw_edited_client_idfs
                                , MAX(hw_edited_date) AS last_edit_dt
                            FROM tbl_hw_edited
                            GROUP BY hw_edited_client_idfs
                      ) AS mx ON het.hw_edited_client_idfs = mx.hw_edited_client_idfs
                                  AND het.hw_edited_date = mx.last_edit_dt
          ) AS he ON i.hw_id = he.hw_edited_client_idfs
      LEFT OUTER JOIN tbl_user AS ue ON he.hw_edited_user_idfs = ue.id

產生以下結果:

| hw_id | hw_hostname | username |              hw_edited_date | username |
|-------|-------------|----------|-----------------------------|----------|
|     1 |   client-01 |  ismaelw | September, 24 2015 23:30:22 |    skalb |
|     2 |   client-02 |  yrumpel | September, 24 2015 20:30:22 |  yrumpel |

看到這個sqlfiddle

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM