I have a MySQL table with following setup:
CREATE TABLE IF NOT EXISTS `coords` (
`project_id` int(4) NOT NULL,
`day` tinyint(4) NOT NULL,
`x` tinyint(4) NOT NULL,
`y` tinyint(4) NOT NULL,
`z` tinyint(4) NOT NULL,
PRIMARY KEY (`tid`,`day`,`x`,`y`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Each project is running several days. Each day some cases are tested and the results (set of coordinates) are saved in this table (along with project id and day number). The same set of x and y coordinates can be saved more often than once (the z coordinate is the actual result).
Now I want to get all available sets of coordinates for a single project with the latest data available.
I.e., assuming I have following data:
pid | day | x | y | z
1 | 1 | 2 | 1 | 5
1 | 1 | 2 | 2 | 6
1 | 2 | 3 | 3 | 7
1 | 3 | 2 | 1 | 8
1 | 3 | 3 | 3 | 9
The query shall now return:
day | x | y | z
1 | 2 | 2 | 6
3 | 2 | 1 | 8
3 | 3 | 3 | 9
I was trying following query:
SELECT day, x, y, z
FROM coords
WHERE pid = 1
GROUP BY CONCAT(tid,'.',x,'.',y)
ORDER BY day DESC
But this query returns old data, just as day = 1, x|y|z = 2|1|5 instead of day = 3 and x|y|z = 2|1|8.
Could someone please point me in the right direction of what I am missing here?
Thanks in advance,
Paul
Try this out :).