我的查询:
$strSQL = "Select customers.*, count(ordersbase.OrderID) As Orders, ordersbase.OrderTime
From customers
Inner Join ordersbase On customers.ID = ordersbase.CustomerID
Group By customers.ID
Order By customers.ID, ordersbase.OrderTime Desc;";
订单表:
我希望将OrderTime
的值作为20181008000000
(降序的最高值),但它返回另一个值:
"0": {
"Orders": 2,
"OrderTime": "20181006000000"
},
答案 0 :(得分:2)
数据以无序方式存储。 Group By
子句中的任何未聚合列(在功能上不依赖于Select
中的列),并且也未在GROUP BY
子句中明确指定,因此是不确定的。您需要使用Max()
函数来获取最新的订购时间。阅读:SELECT list is not in GROUP BY clause and contains nonaggregated column .... incompatible with sql_mode=only_full_group_by
Order by
在完成分组后被处理。因此,您无法使用Order by
。
请尝试以下操作:
$strSQL = "Select customers.*,
count(ordersbase.OrderID) As Orders,
MAX(ordersbase.OrderTime)
From customers
Inner Join ordersbase On customers.ID = ordersbase.CustomerID
Group By customers.ID
Order By customers.ID";