我有一个包含数据的 MySQL 表,并且想为进行的特定测试选择最新条目。
table: research
columns: id (int), projectid (int), test (varchar),
when (datetime), result (longtext)
行示例:
1, 6, "Test1", 01-01-2013 12:15, "AAAAA"
2, 6, "Test1", 01-01-2013 13:15, "BBBBB"
3, 6, "Test2", 01-01-2013 16:00, "CCCCC"
4, 6, "Test2", 01-01-2013 16:15, "DDDDD"
5, 6, "Test2", 01-01-2013 16:30, "EEEEE"
如果我想要“测试 1”的最新结果,我使用..
SELECT *
FROM research
WHERE projectid=6 AND test='Test1'
ORDER BY when DESC limit 1
但是如何在一个查询中获得单个项目中每个测试的最新结果?我试过:
SELECT research.*
FROM research INNER JOIN (SELECT MAX(id) AS id
FROM research
WHERE projectid=".$ProjectId."
GROUP BY test) ids
ON research.id = ids.id
WHERE research.projectid=".$ProjectId
但它没有给我最新的测试结果,它给了我最古老的..