Jump to content

[SOLVED] Probably simple question about dates.


Recommended Posts

Your first step would be to use a DATE data type. Your current scheme will require a slow complex query to accomplish any comparison, sorting, or retrieval of the data. Your scheme also requires 6 bytes to store what a DATE type stores in 3 bytes.

Backup your database; add a new DATE type column to the table; populate it with the equivalent date values from the existing data in the three columns; modify the query that inserts into the three columns to use a mysql STR_TO_DATE() function to put the same data into the new DATE type column; modify any query that retrieves from the three columns to use a mysql DATE_FORMAT() function to retrieve from the DATE type column in any format you want; test everything; delete the three smallint columns. To populate the new DATE type column from the existing data, use the mysql STR_TO_DATE() function in a simple UPDATE query.

 

Backup your database; add a new DATE type column to the table; populate it with the equivalent date values from the existing data in the three columns; modify the query that inserts into the three columns to use a mysql STR_TO_DATE() function to put the same data into the new DATE type column; modify any query that retrieves from the three columns to use a mysql DATE_FORMAT() function to retrieve from the DATE type column in any format you want; test everything; delete the three smallint columns. To populate the new DATE type column from the existing data, use the mysql STR_TO_DATE() function in a simple UPDATE query.

 

 

cmon man it requires about a 10,000 records to make a 5 sec difference in processing and its numerical so it will be quick, rember were XP developers in web apps that are comercial

This could be done in a query with the existing scheme, but it will be slow as the two columns (I guess the day is not include) need to be retrieved from every row in the table and compared. A DATE type only requires that a single value be compared. Doing this in some slow parsed/tokenized/interpreted php code will be even slower.

 

It is always better to fix underlying problems as early as possible. Using a DATE type will also allow you to use 20-30 mysql date functions that will make doing everything with this date easier.

 

Fixing this problem will result in a simple WHERE clause in the query to get the data  -

 

... WHERE the_date_column >= CURDATE() - INTERVAL 6 MONTH

I didn't want to show what i've tried already, because I thought there might be a better way, but I was trying something like htis.....

 

SELECT *

FROM stats

WHERE DATE(CONCAT('01', '-', month, '-', year)) > datesub(curdate(), interval 6 month)

 

But obviously it doesn;t work, and I could not get it to...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.