I’m using MySQL and I have a table that has dates in a column (I’m making a table of due dates for a class). Previously I had the query as follows:
SELECT * FROM duedates_f2011 ORDER BY anothercolumn
In order to prevent duplicate occurrences (based on the duedate column) I had to change it as follows:
SELECT * FROM duedates_f2011 GROUP BY duedate
Currently, it’s retained the same order, but how can I assure that it will still do so? Nothing in the GROUP BY tutorials I’ve read indicates that it will still sort it, and adding an ORDER BY statement afterwards is incorrect. Is there something I’m missing, or is there something else I should add to make sure it remains in order?
(as indicated in my tags, I’m using PHP to run the query)
You can use
group by ... order by ...(in this order). If you use justgroup by, it will be ordered automatically by that column(s).However, if you want to
group by duedateandorder by anothercolumn, realize one thing: the sql doesn’t know which value to take for anothercolumn, when you group by duedate…edit: from the mysql manual