简体   繁体   English

如何使用MySQL从max(datetime)获取对应的值?

[英]How to get corresponding value from max(datetime) with MySQL?

MySQL software table: MySQL软件表:

.------------------------------------------------------------.
| id | hostname   | software | version | install_date        |
|------------------------------------------------------------|
| 1  | computer-a | vim      | 1.00    | 2012-01-04 00:00:00 | 
|------------------------------------------------------------|
| 1  | computer-a | vim      | 2.00    | 2012-01-05 00:00:00 | 
|------------------------------------------------------------|
| 1  | computer-b | emacs    | 2.00    | 2012-01-04 00:00:00 | 
|------------------------------------------------------------|
| 1  | computer-b | emacs    | 1.00    | 2012-01-05 00:00:00 | 
|------------------------------------------------------------|
| 1  | computer-c | emacs    | 2.00    | 2012-01-05 00:00:00 | 
|------------------------------------------------------------|
| 1  | computer-c | vim      | 3.00    | 2012-01-05 00:00:00 | 
'------------------------------------------------------------'

MySQL query: MySQL查询:

SELECT 
  hostname,
  MAX(IF(software = 'vim', version, NULL)) AS VIM,
  MAX(IF(software = 'emacs', version, NULL)) AS EMACS
FROM
  software
GROUP BY
  hostname;

Result: 结果:

+-------------+---------+----------+
| hostname    | VIM     | EMACS    |
+-------------+---------+----------+
| computer-a  | 2.0     | NULL     |
+-------------+---------+----------+
| computer-b  | NULL    | 2.0      |
+-------------+---------+----------+
| computer-c  | 3.0     | 2.0      |
+-------------+---------+----------+

http://www.sqlfiddle.com/#!8/6247d/3 http://www.sqlfiddle.com/#!8/6247d/3

The problem is that I want to show the versions with the most recent install_date for each software . 问题是我想显示每个software最新install_date版本。 Computer-b should be showing 1.0 for EMACS. Computer-b对于EMACS应该显示1.0。 How can the query be changed to accomplish this? 如何更改查询以实现此目的?

I can change the query to: 我可以将查询更改为:

SELECT 
  hostname,
  MAX(IF(software = 'vim', install_date, NULL)) AS VIM,
  MAX(IF(software = 'emacs', install_date, NULL)) AS EMACS
FROM
  software
GROUP BY
  hostname;

This pulls the right install_date but how can I show the corresponding version ? 这可以拉正确的install_date,但是如何显示相应的version

http://www.sqlfiddle.com/#!8/6247d/4 http://www.sqlfiddle.com/#!8/6247d/4

You can use a subquery to get the max(install_date) for each hostname and then join that back to your table: 您可以使用子查询来获取每个hostnamemax(install_date) ,然后将其重新加入表中:

select s1.hostname,
  MAX(IF(software = 'vim', version, NULL)) AS VIM,
  MAX(IF(software = 'emacs', version, NULL)) AS EMACS
from software s1
inner join
(
  select max(install_date) i_date,
    hostname
  from software
  group by hostname
) s2
  on s1.install_date = s2.i_date
  and s1.hostname = s2.hostname
group by s1.hostname

See SQL Fiddle with Demo . 请参阅带有演示的SQL Fiddle The result is: 结果是:

|   HOSTNAME |    VIM |  EMACS |
--------------------------------
| computer-a |   2.00 | (null) |
| computer-b | (null) |   1.00 |
| computer-c |   3.00 |   2.00 |

You might need to group by both the hostname and software in the subquery if the software could have different install_date : 如果software可以具有不同的install_date则可能需要在子查询中对hostnamesoftware进行group by

select s1.hostname,
  MAX(IF(s1.software = 'vim', version, NULL)) AS VIM,
  MAX(IF(s1.software = 'emacs', version, NULL)) AS EMACS
from software s1
inner join
(
  select max(install_date) i_date,
    hostname, software
  from software
  group by hostname, software
) s2
  on s1.install_date = s2.i_date
  and s1.hostname = s2.hostname
group by s1.hostname

See SQL Fiddle with Demo 参见带有演示的SQL Fiddle

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

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