简体   繁体   中英

Select last row in MySQL statment

here is the mysql statement :

SELECT 
MAX(`history_card`.`PART_NUMBER`)  AS `PART_NUMBER`, `history_card`.`SERIAL_NUMBER`, 
MAX(`history_card`.`POSITION`) AS `POSITION`, 
MAX(`history_card`.`RELEASE_DATE_TO_AIRCRAFT`)  AS `RELEASE_DATE_TO_AIRCRAFT`,
MAX(`history_card`.`DATE_OFF_AIRCRAFT`)  AS `DATE_OFF_AIRCRAFT`, 
MAX(`history_card`.`LAST_CAP_CHECKED_DATE`)  AS `LAST_CAP_CHECKED_DATE`, 
MAX(`history_card`.`DUE_CAP_CHECK_DATE`) AS `DUE_CAP_CHECK_DATE`, 
MAX(`history_card`.`JOB_REMARKS`) AS `JOB_REMARKS`, 
MAX(`history_card`.`TSO`) AS `TSO`, 
MAX(`history_card`.`BO_NUMBER`) AS `BO_NUMBER`, 
MAX(`history_card`.`REPAIR_ORDER_NUMBER`) AS `REPAIR_ORDER_NUMBER`, 
MAX(`history_card`.`LAST_OVERHAULED_DATE`) AS `LAST_OVERHAULED_DATE`, 
MAX(`history_card`.`DUE_OVERHAUL_DATE`) AS `DUE_OVERHAUL_DATE`, 
MAX(`history_card`.`REFRESHER_DATE`) AS `REFRESHER_DATE`, 
MAX(`history_card`.`REFRESHER_DONE`) AS `REFRESHER_DONE`, 
MAX(`history_card`.`GRN_ISSUE_DATE`) AS `GRN_ISSUE_DATE`, 
MAX(`history_card`.`WORKSHEET`) AS `WORKSHEET`, 
MAX(`history_card`.`ADDITIONAL_ATTACHMENT`) AS `ADDITIONAL_ATTACHMENT`, 
MAX(`history_card`.`GRN`) AS `GRN`
FROM `history_card`
GROUP BY `history_card`.`SERIAL_NUMBER`
HAVING (((MAX(`history_card`.`DUE_CAP_CHECK_DATE`)) BETWEEN  NOW() AND DATE_SUB(NOW(), INTERVAL -60 DAY) ))
ORDER BY MAX(`history_card`.`DUE_CAP_CHECK_DATE`) DESC;

Instead of MAX() I need to select the last row , meaning that the ID DESC has to be selected and limited to 1 record : ORDER BY id DESC LIMIT 1 but right now It is returning the max value , Coz I'm converting this query from MS , MS used LAST() function to get the last record by in mysql I need a similar solution for MySQL

If I understood the problem correctly, something on these lines should work:

SELECT 
`history_card`.`SERIAL_NUMBER`, 
`history_card`.`PART_NUMBER`  AS `PART_NUMBER`, 
`history_card`.`POSITION` AS `POSITION`, 
`history_card`.`RELEASE_DATE_TO_AIRCRAFT`  AS `RELEASE_DATE_TO_AIRCRAFT`,
`history_card`.`DATE_OFF_AIRCRAFT`  AS `DATE_OFF_AIRCRAFT`, 
`history_card`.`LAST_CAP_CHECKED_DATE`  AS `LAST_CAP_CHECKED_DATE`, 
`history_card`.`DUE_CAP_CHECK_DATE` AS `DUE_CAP_CHECK_DATE`, 
`history_card`.`JOB_REMARKS` AS `JOB_REMARKS`, 
`history_card`.`TSO` AS `TSO`, 
`history_card`.`BO_NUMBER` AS `BO_NUMBER`, 
`history_card`.`REPAIR_ORDER_NUMBER` AS `REPAIR_ORDER_NUMBER`, 
`history_card`.`LAST_OVERHAULED_DATE` AS `LAST_OVERHAULED_DATE`, 
`history_card`.`DUE_OVERHAUL_DATE` AS `DUE_OVERHAUL_DATE`, 
`history_card`.`REFRESHER_DATE` AS `REFRESHER_DATE`, 
`history_card`.`REFRESHER_DONE` AS `REFRESHER_DONE`, 
`history_card`.`GRN_ISSUE_DATE` AS `GRN_ISSUE_DATE`, 
`history_card`.`WORKSHEET` AS `WORKSHEET`, 
`history_card`.`ADDITIONAL_ATTACHMENT` AS `ADDITIONAL_ATTACHMENT`, 
`history_card`.`GRN` AS `GRN`
FROM `history_card`
JOIN 
(
SELECT MAX(`history_card`.`ID`) ID, `history_card`.`SERIAL_NUMBER`
FROM `history_card`, 
WHERE `history_card`.`DUE_CAP_CHECK_DATE` BETWEEN  NOW() AND DATE_SUB(NOW(), INTERVAL -60 DAY) 
GROUP BY `history_card`.`SERIAL_NUMBER`
) HC 
ON `HC`.`ID`=`history_card`.`ID`
AND `HC`.`SERIAL_NUMBER`=`history_card`.`SERIAL_NUMBER`
SELECT TOP 1 * FROM .... ORDER BY ID DESC

应该会给您最后的记录。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM