2

我有一个关于从表中获取最新记录的经典问题。这是声明:

SELECT t1.Item, t1.Price, t1.ODate
FROM Order AS t1
JOIN ( (SELECT Item, Price, Max(ODate) As MaxDate
        FROM Order
        GROUP BY Item ) As t2 )
ON t1.Item = t2.Item and t1.Odate = MaxDate
ORDER BY t1.Item ASC

此语句适用于 phpMyAdmin 和 LibreOffice base,但 LibreOffice basic 给我一条消息:SQL 表达式中的语法错误。

有什么帮助吗?

4

1 回答 1

1

也许您应该从 t2 的选择中删除“价格”。加入似乎是无稽之谈,并且会在 MSSMS 2008 中导致错误消息,就我而言。

选择列表中的“Order.Price”列无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。

尝试

SELECT t1.Item, t1.Price, t1.ODate
FROM Order AS t1
JOIN  (SELECT Item, Max(ODate) As MaxDate
        FROM Order
        GROUP BY Item ) As t2 
ON t1.Item = t2.Item and t1.Odate = MaxDate
ORDER BY t1.Item ASC
于 2013-04-20T03:15:16.747 回答