简体   繁体   中英

PHP mysql Order by and Group By query

In my order history, I am currently listing all the customers previous orders, however, not in date order. My current query is :

SELECT OrderItemID, Date
FROM orderitems
WHERE OrderID=$orderid
GROUP BY OrderID;

After this query, I use a while loop, for each order found, to print the past order in a table. However I want to print past orders by 'ORDER BY Date'. After the GROUPBY I put ORDER BY Date Desc, but had no difference in results printing.

Any guidance would be appreciated.

Since you are filtering the records with OrderID , why do you need to GROUP BY it?

SELECT OrderItemID, Date
FROM orderitems
WHERE OrderID=$orderid
ORDER BY Date DESC

I'm not sure why OrderItems would have the date of the order. Based on the text, I suspect you want something like this:

SELECT OrderID, Date
FROM Orders o
WHERE CustomerId = $CustomerId
ORDER BY Date DESC;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM