Last MySQL record from table

How can I select the last records (= MAX (order_history_id) from this table (one [last] row on order_id):

+----------------+--------+-------+----------+
|order_history_id|order_id|trackc |date_added|
+----------------+--------+-------+----------+
|      4400      |   1000 | text  | 2014-9-24|
|      4401      |   1001 | text2 | 2014-9-26|
|      4410      |   1000 | text3 | 2014-9-29|
|      4411      |   1003 | text4 | 2014-9-20|
+----------------+--------+-------+----------+

My current request:

SELECT * FROM order_history WHERE  trackc <>  '' GROUP BY order_id
+4
source share
3 answers
select t1.* from order_history t1
inner join(
    select order_id,max(order_history_id) as order_history_id
    from order_history
    where ifnull(trackcode,'') <> ''
    group by order_id
) as t2 on t1.order_history_id = t2.order_history_id
+3
source
SELECT * from order_history as oh inner join (
    SELECT MAX(order_history_id) AS max_id, order_id
    FROM order_history
    GROUP BY order_id
) as t ON t.max_id = oh.order_history_id
WHERE oh.trackc <>  ''

The subquery retrieves the maximum value for each order_id. It then joins the found values ​​with order_history and retrieves all the fields.

+1
source
 select or.order_history_id,or.order_id,or.trackc,or.date_added From order_history or
 order by or.order_id Desc
 limit 1;
-1
source

Source: https://habr.com/ru/post/1609219/


All Articles