如何通过获取最后一条数据记录逐项加入t_detail和t_header mysql?
id | doc | item | price
1 | 002 | asz | 1200
2 | 002 | aqw | 3000
3 | 002 | qwe | 1300
4 | 005 | qwe | 1000
5 | 005 | aqw | 2000
输出
id | doc | item | price
1 | 002 | asz | 1200
4 | 005 | qwe | 1000
5 | 005 | aqw | 2000
答案 0 :(得分:2)
试试这个,
SELECT * FROM table WHERE id IN (SELECT MAX(id) FROM table GROUP BY item)
答案 1 :(得分:1)
试试这个:扩展@Kushan的答案
SELECT * FROM table t1
WHERE id IN (SELECT MAX(id) FROM table t2
WHERE t2.item = t1.item GROUP BY item )
答案 2 :(得分:0)
Use GROUP BY and JOIN :
SELECT id,doc,item,price
FROM Table1
JOIN
(
SELECT MAX(Id) _Id,item
FROM Table1
GROUP BY item
) A ON A._Id = id
答案 3 :(得分:0)
获得更多表现:
SELECT
d.*
FROM
t_detail d INNER JOIN
(
SELECT
MAX(id) as 'id'
FROM t_detail
GROUP BY item
) dmax on d.id = dmax.id ;
这样查询只执行一次。