简体   繁体   English

如何在 MYSQL LEFT JOIN 中选择最新的 DATE

[英]how to select latest DATE in MYSQL LEFT JOIN

i have 2 MYSQL tables that i LEFT JOIN based on the machine id column ('mid')我有 2 个 MYSQL 表,我根据机器 id 列('mid') LEFT JOIN

machines' table机台

+-----+-------------+-------------+-----+---------------------+
| mid | machineName |  ipAddress  | nid |      modified       |
+-----+-------------+-------------+-----+---------------------+
|   1 | machine1    | 192.168.1.1 |  16 | 2018-01-28 19:48:53 |
|   2 | machine2    | 192.168.2.1 |  16 | 2018-01-29 16:28:11 |
+-----+-------------+-------------+-----+---------------------+

'user_reverts' table 'user_reverts' 表

+----+-----+-----+---------------------+
| id | uid | mid |      modified       |
+----+-----+-----+---------------------+
| 11 | 189 |  1  | 2018-01-30 10:01:10 |
| 13 | 189 |  2  | 2018-01-30 12:11:50 |
| 14 | 190 |  1  | 2018-01-26 17:09:17 |
| 15 | 190 |  2  | 2018-01-25 15:51:31 |
| 16 | 189 |  2  | 2018-01-30 19:48:58 |
+----+-----+-----+---------------------+

the query counts the number of reverts the user has made on each machine since the beginning of each day and groups it by machine.该查询计算自每天开始以来用户在每台机器上进行的还原次数,并按机器对其进行分组。

SELECT machines.mid as mid, machines.machineName as machineName,
        machines.ipAddress as ipAddress, coalesce(reverts.count, 0) as count, 
        coalesce(reverts.modified,'never') as reverted FROM
            (SELECT mid, machineName, INET6_NTOA(ipAddress) as ipAddress FROM machines) machines
            LEFT JOIN
            (SELECT machines.mid,user_reverts.modified,count(*) as count FROM machines
            INNER JOIN user_reverts ON user_reverts.mid=machines.mid AND user_reverts.uid = 189
            WHERE DATE(user_reverts.modified) = CURDATE() group by user_reverts.mid) reverts 
            ON reverts.mid = machines.mid

it works well except i want the 'reverted' column to show the latest time and date, so the expected result would be它工作得很好,除了我希望“恢复”列显示最新的时间和日期,所以预期的结果是

+-----+-------------+-------------+-------+---------------------+
| mid | machineName |  ipAddress  | count |      reverted       |
+-----+-------------+-------------+-------+---------------------+
|   1 | machine1    | 192.168.1.1 |     1 | 2018-01-24 10:01:10 |
|   2 | machine2    | 192.168.2.1 |     2 | 2018-01-30 19:48:58 | (latest)
+-----+-------------+-------------+-------+---------------------+

i tried to replace我试图更换

coalesce(reverts.count, 0)

with

MAX(reverts.count, 0)

but i get an error message.但我收到一条错误消息。 how do i do this right ?我该怎么做? Thanks谢谢

You can add another column and simplify your query allalong:您可以添加另一列并简化您的查询:

        SELECT machines.mid as mid
         , machines.machineName as machineName
         , INET6_NTOA(ipAddress) as ipAddress
         , coalesce(count(*), 0) as count 
         , coalesce(MAX(user_reverts.modified),'never') as reverted  -- latest revert 
      FROM machines
 LEFT JOIN user_reverts
        ON ( user_reverts.mid=machines.mid AND user_reverts.uid = 189 )
     WHERE DATE(user_reverts.modified) = CURDATE()
  GROUP BY machines.mid
         ;

Have a look at this SQLFiddle (The exact table definitions may differ from yours, of course).看看这个 SQLFiddle (当然,确切的表定义可能与您的不同)。

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

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