简体   繁体   English

MySQL查询-加入子查询

[英]MySQL Query - Join Subquery

What I have 我有的

I have multiple Tables like this: 我有多个这样的表:

  • tbl_hw_inventar (hw_id, hostname, hw_typ_idfs, hw_created_user_idfs, etc...) tbl_hw_inventar(hw_id,主机名,hw_typ_idfs,hw_created_user_idfs等)
  • tbl_hw_typ (hw_typ_id, hw_typ_title, etc...) tbl_hw_typ(hw_typ_id,hw_typ_title等)
  • tbl_user (user_id,username, etc...) tbl_user(用户ID,用户名等)
  • tbl_hw_edited (hw_edited_id, hw_edited_client_idfs, hw_edited_date, hw_edited_user_idfs) tbl_hw_edited(hw_edited_id,hw_edited_client_idfs,hw_edited_date,hw_edited_user_idfs)

What I need 我需要的

I want output a table with the following information: 我要输出包含以下信息的表:

 hw_id | Hostname | created                         | last edited 

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

The problem 问题

As you can see, I can get information like the hw_typ with a foreign key and a inner join to the " tbl_hw_typ ". 如您所见,我可以获得带有外键和对“ tbl_hw_typ ”的内部 tbl_hw_typ之类的信息。 The same for information with " hw_created_user_idfs " and a inner join to get the username for the userid . 对于带有“ hw_created_user_idfs ”和内部hw_created_user_idfs以获取用户 ID 用户名的信息也是如此。

But how can I get the last edited , datetime and the username ? 但是,如何获取最后编辑的 日期时间用户名

In my table " tbl_hw_edited " i have entries like this: 在我的表“ tbl_hw_edited ”中,我有这样的条目:

row id | hw_id | datetime | user_id

The code 编码

My SQL query looks like this so far: 到目前为止,我的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

So how can I export the information? 那么如何导出信息? It looks like I have to make a subquery in my query. 看来我必须在查询中进行子查询。 But I failed with every try. 但是我每次尝试都失败了。

Thanks for you help 谢谢你的帮助

EDIT 编辑

As proposed I'm providing more information (table data) for each table: 按照提议,我将为每个表提供更多信息(表数据):

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

So as a final result I need an output like this: 因此,作为最终结果,我需要这样的输出:

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

If I have interpreted your tables correctly, you need to find the max(edit date) for each hw_edited_client_idfs (which joins to hw_id) 如果我正确地解释了您的表,则需要为每个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

With that result you may join it back to the source table, to discover which user is associated to the max(edit date) 通过该结果,您可以将其重新连接到源表,以发现与最大(编辑日期)关联的用户

                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

Then this result used like so: 然后,该结果使用如下:

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

which produces this result: 产生以下结果:

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

see this sqlfiddle 看到这个sqlfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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