Would it be possible to populate the select box with the distinct Month and Year together and then be able to query the database for the selected month/year combination? Right now it’s set up using two select boxes (one for both month and year) to where the user has to select a Month and a Year to view the reports for the month. The downside to this is that there are some months that did not sell any items so the report for that month would come back empty.
I would like them to only have to select one select box that will have the month and year value already set. This way every report they select will have data. Is this possible?
SELECT DISTINCT
DATEPART(year, orderDate) as orderyear
, DATEPART(month, orderdate) as ordermonth
FROM orderTable
ORDER BY orderdate
would ordinarily not pass muster if this were any database other than mysql, which is notorious, dare i say infamous, for allowing really bad syntax to run
year and month are extracted from the table
so first, you get
2004 01
2004 01
2004 01
2004 01
2004 02
2004 02
2004 02
2004 02
2004 02
2004 02
2004 02
and so on
next, the database has to sort all those rows into row sequence, so as to be ablt to detect and eliminate duplicates
thus, the result of the DISTINCT phase is
2004 01
2004 02
and so on
at this point the database is now ready to sort these results into the requested sequence as stated in the ORDER BY clause
which should be a syntax error
but it’s hardly your fault, i mean, if mysql runs it, how will you ever learn that it’s wrong?