MySQL问题降序

时间:2018-10-10 13:35:52

标签: php mysql

我的查询:

$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;";

订单表:

enter image description here

我希望将OrderTime的值作为20181008000000(降序的最高值),但它返回另一个值:

    "0": {
        "Orders": 2,
        "OrderTime": "20181006000000"
    },

1 个答案:

答案 0 :(得分:2)

请尝试以下操作:

$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";