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
每个software
. Computer-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 但我怎样才能显示相应的version
?