MySQL查询 - 加入子查询

时间:2015-09-28 06:57:16

标签: mysql join subquery

我有什么

我有多个这样的表:

  
      
  • tbl_hw_inventar(hw_id,hostname,hw_typ_idfs,hw_created_user_idfs等...)
  •   
  • tbl_hw_typ(hw_typ_id,hw_typ_title等......)
  •   
  • tbl_user(user_id,username等等)
  •   
  • 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) |

问题

正如您所看到的,我可以通过外键内部联接获取hw_typ等信息到" tbl_hw_typ&# 34 ;.对于" hw_created_user_idfs"的信息也是如此。以及内部联接以获取用户ID 用户名

但我怎样才能获得上次修改日期时间用户名

在我的表格中#34; 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) |

1 个答案:

答案 0 :(得分:1)

如果我正确解释了你的表,你需要找到每个hw_edited_client_idfs(加入hw_id)的最大值(编辑日期)

                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 |

产生这个结果:

def myfunction():
   for file in files:
      myfun(file, Temp=True)

请参阅this sqlfiddle