我已经花了几个小时试图解决这个问题,但没有运气。我试图获取机器状态的最新更改。机器具有唯一的名称,属于几个不同的类。以下查询是我最接近我想要的,但它没有返回最新结果。只是不同的类和名称值。
SELECT * FROM ( SELECT * FROM MachineStatus ORDER BY date DESC ) AS ord GROUP BY class, name
数据表看起来像这样,但行混合了。
Class Name Status Date A NXP ONLINE 2015-02-17 A NXP OFFLINE 2015-02-10 A AVR STANDBY 2015-02-16 A AVR OFFLINE 2015-01-05 A PIC ONLINE 2015-02-01 A PIC STANDBY 2015-01-05 B TXI ONLINE 2015-02-10 B TXI STANDBY 2015-02-07 B FSC ONLINE 2015-02-17 B FSC OFFLINE 2015-02-01 C STM OFFLINE 2015-02-17 C STM STANDBY 2015-02-10 C INT ONLINE 2015-01-01 C INT STANDBY 2014-11-10
我想要得到的是:
A NXP ONLINE 2015-02-17 A AVR STANDBY 2015-02-16 A PIC ONLINE 2015-02-01 B TXI ONLINE 2015-02-10 B FSC ONLINE 2015-02-17 C STM OFFLINE 2015-02-17 C INT ONLINE 2015-01-01
非常感谢!
答案 0 :(得分:0)
SELECT t.Class, t.Name, t.Status, t.Date
FROM my_table t
INNER JOIN (SELECT Class, Name, MAX(Date) AS Date
FROM my_table
GROUP BY Class, Name
) sub ON (sub.Class = t.Class AND sub.Name = t.Name AND sub.Date = t.Date)
GROUP BY t.Class, t.Name
答案 1 :(得分:0)
应该像......一样直截了当。
SELECT `table`.* FROM `table` GROUP BY `name` HAVING `date` = MAX(`date`)