如何从此表中选择最新记录(= MAX(order_history_id)(每个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|
+----------------+--------+-------+----------+
我目前的查询是:
SELECT * FROM order_history WHERE trackc <> '' GROUP BY order_id
答案 0 :(得分:3)
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
答案 1 :(得分:1)
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 <> ''
子查询提取每个order_id的最大值。比使用order_history连接找到的值并提取每个字段。
答案 2 :(得分:-1)
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;