gpt4 book ai didi

mysql - 如何使用MySQL从max(datetime)中获取相应的值?

转载 作者:行者123 更新时间:2023-11-29 13:57:03 25 4
gpt4 key购买 nike

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 查询:

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

结果:

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

问题是我想显示每个软件的最新install_date版本。计算机 b 的 EMACS 应显示 1.0。如何更改查询来实现此目的?

我可以将查询更改为:

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;

这会提取正确的 install_date 但我如何显示相应的版本

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

最佳答案

您可以使用子查询获取每个主机名max(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

参见SQL Fiddle with Demo 。结果是:

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

如果软件可以有,您可能需要在子查询中对主机名软件进行分组不同的安装日期:

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

参见SQL Fiddle with Demo

关于mysql - 如何使用MySQL从max(datetime)中获取相应的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15694618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com