Zakir_SZH
Zakir_SZH

Reputation: 486

MySQL issue with descending order

My query :

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

Orders table:

enter image description here

I expected to get the OrderTime value as 20181008000000 (highest value in descending order) but it returns the other one:

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

Upvotes: 1

Views: 72

Answers (1)

Madhur Bhaiya
Madhur Bhaiya

Reputation: 28844

Try the following instead:

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

Upvotes: 2

Related Questions