scene ： A waybill , If you can stock in multiple times , There will be logs of multiple warehousing records
demand ： Now you need to get the latest log of the warehousing record
<>1, stay WHERE Mode of using subquery after condition
* In that case , These conditions can be directly placed in the sub query , And then accurately return the specified latest id, Used in the outer layer id Filter out a piece of data
* If it is id Self accretion , Just use it MAX(id), If not id Self accretion , Then you need to use MAX(create_time) And other fields to get the latest record SELECT *
FROM tms_waybill_log log WHERE log.id = ( SELECT MAX(id) FROM tms_waybill_log
WHERE dr = 0 AND waybill_id = 125 AND waybill_status = 1010 )
<>2, use ORDER BY Mode of
* use ORDER BY Sort them in reverse order , Then take out the first one , It's the latest
* use ORDER BY Hour , Will execute first WHERE
, Filter out some data , Then sort these data , If there is more filtered data , Then the efficiency is relatively low , If the filtered data is relatively small , Then it's almost no effect , Very efficient SELECT *
FROM tms_waybill_log log WHERE log.dr = 0 AND log.waybill_id = 125 AND log.
waybill_status= 1010 ORDER BY log.id DESC LIMIT 1
If there are other methods , Welcome to leave a message at the bottom !