MySQL software table:
.------------------------------------------------------------.
| 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:
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
The problem is that I want to show the versions with the most recent install_date
for each software
. Computer-b should be showing 1.0 for EMACS. 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
?
You can use a subquery to get the max(install_date)
for each hostname
and then join that back to your table:
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 . 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
:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.