delimiter $$
CREATE TABLE `tbl_locate` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Locating` bit(1) DEFAULT NULL,
`MainPower` bit(1) DEFAULT NULL,
`Acc` bit(1) DEFAULT NULL,
`PowerOff` bit(1) DEFAULT NULL,
`Alarm` int(11) DEFAULT NULL,
`Speed` int(11) DEFAULT NULL,
`Direction` int(11) DEFAULT NULL,
`Latitude` double DEFAULT NULL,
`Longitude` double DEFAULT NULL,
`DateTime` datetime DEFAULT NULL,
`MainID` int(11) DEFAULT NULL,
`IOState` int(11) DEFAULT NULL,
`OilState` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1$$
SQL STRING
SELECT * FROM tbl_locate WHERE DateTime=(SELECT MAX(`DateTime`) FROM tbl_locate WHERE MainID=1,2,3)
i want to return the data which is latest row where main id are 1 or 2 or 3,
so,need to return 3 rows latest datetime.
1st row are id 1 latest datetime
second row are id 2 latest datetime
third row are id 3 latest datetime
How to do it?
try this