Not sure what to search for, so this may be a duplicate question. I have a mysql database with a single table called software. The software table is comprised of the following fields:
id (auto_increment)
computer_name
software_name
software_version
install_date
Every time a piece of software is installed, upgraded or reverted to a previous version a new entry is made into the table.
I'm trying to build a table with php that would look like so:
.---------------------------------------------------.
| | software x | software y | software z |
|---------------------------------------------------|
| computer a | ver 1.0 | | ver 1.2 |
|---------------------------------------------------|
| computer b | | ver 2.1 | |
|---------------------------------------------------|
| computer c | ver 1.3 | ver 1.1 | |
'---------------------------------------------------'
From data like so:
computer_name | software_name | software_version | install_date
----------------------------------------------------------------------
computer a | software x | ver 1.1 | [10 days ago]
computer a | software x | ver 1.0 | [2 days ago]
computer a | software z | ver 1.1 | [20 days ago]
computer a | software z | ver 1.2 | [5 days ago]
The install_date
would be an actual date, threw in [X days ago] for convenience.
The table would show the latest version of each software installed on each computer, with only one row for each unique computer entry and one column for each unique software entry.
Is this possible to do with a single MySQL table? What would be the best way to accomplish this? I'm using PHP and MySQL with codeigniter.
Update:
From looking at the possible duplicate question I was able to prepare this query:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(software_name = ''',
software_name,
''', software_version, NULL)) AS ',
replace(software_name, ' ', '_')
)
) INTO @sql
FROM
software;
SET @sql = CONCAT('SELECT computer_name, ', @sql, ' FROM software GROUP BY computer_name');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Which generates results in the format I'm looking for, however it is not getting the version by the latest install_date
but rather by the biggest version number found. How can I modify the above to get the version with the newest associated install_date
?