Forum Moderators: open

Message Too Old, No Replies

MySQL

Help with MySQL query

         

Alternative Future

9:02 am on Aug 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



MySQL 4.0.23-standard is the version I am using

Hi all,

Looking for some help in constructing this query, and appreciate all help.

I am about to create a new table, this table is going to have a few columns, but the main ones are going to be date_from and date_to.
First question what is the best type for this column i.e DATE or DATETIME.
Now my main queries on this table are going to be firstly checking todays date and getting all results from table that are >= (after and equal to). I then want to also GROUP BY months so that the results will show all columns in current month then move on to next month and so on, also these results should be in order of nearest to query date. i.e.
CURRENT MONTH
1 - Result from todays date ending 25th August
2 - " " " " 19th August 2005 ending 26th August
3 - " " " " 26th August 2005 ending 1st September
SEPTEMBER
4 - Result from 1st Sept ending 7th Sept
and so on

Note: in the first GROUP we have a column with an end date in September but the beginning date belongs in the first month, so this would mean that the GROUP BY used the date_from column when GROUPING.

My question is what would my SQL statement look like to get the results in a format like this?

SELECT * FROM mytable WHERE (some date functions?) = (CURRDATE or something?) GROUP BY (column name?) ORDER BY (date_from?)

TIA,

-George

txbakers

1:49 pm on Aug 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since you won't be storing the time, I'd recommend just the DATE field type.

It looks like you might need two queries to do what you want to.

The date functions are pretty good, and you can group by MONTH(datefld) and put that in your select clause.

The reason I think two queries is that you want to separate current month from other months, and you might have teo specify that in the WHERE, unless you have some type of row checking after the query is executed.

What I do for these complex situations is hardcode as much of the query as possible to get a fixed dataset. Then, I go back and put variables in one at a time until it works for all query possibilities.

Give that a try here and see if it works.

Alternative Future

1:55 pm on Aug 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi txbakers,

Can you explain a little more on this:

>>unless you have some type of row checking after the query is executed

TIA,

-George

coopster

8:53 pm on Aug 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The reference is to how you process and format the result set once it is returned. I would run a single query and use your application logic to process and format.
SELECT 
MONTH(date_to) as month,
DATE_FORMAT(date_from, '%D %M %Y') AS date_from,
DATE_FORMAT(date_to, '%D %M %Y') AS date_to
FROM times
ORDER BY date_to
;

Your result set of data would then require you to loop through monitoring for a change in the month. Note, you may actually want to add year to that as well in case you ever had a jump from say January 2005 to January 2006 without any months in between.

Alternative Future

7:44 am on Aug 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks coopster,

Like you said I shall keep the separation of months and years to the application logic by splitting it up into month and year arrays.

Many thanks for the query...

-George