早上好,
我正在尝试开发一个简单的库存系统,但是我很难选择每个产品的最后一个条目(组件;参考)。
我目前的查询是:
SELECT component, ref, date, qty FROM $usertable WHERE (SELECT qty FROM $usertable HAVING max(date) ORDER BY component ASC, ref ASC LIMIT 0,1)
正如您所看到的,我不熟悉嵌套查询:/
有人可以帮我找出解决方案吗?
答案 0 :(得分:2)
您可以使用自我加入来获取每个产品的最后一个条目
SELECT u1.*
FROM
$usertable u1
JOIN (
SELECT component, ref, MAX(date) date
FROM $usertable
GROUP BY component, ref
) u2
USING(component, ref,date)
答案 1 :(得分:0)
试试这个:
SELECT component, ref, date, qty FROM
( SELECT component, ref, date, qty FROM MyTABLE ORDER BY date DESC ) AS dummyTable
GROUP BY component, ref
答案 2 :(得分:0)
试试这段代码:
SELECT DISTINCT component, ref, date, qty
FROM $usertable
ORDER BY date