Jump to content

Get all Months before current month?


aesthetics1

Recommended Posts

This may sound a little complicated.. I'm fairly new to some of these concepts so please excuse me if there is a much easier way...

 

Basically, I am looking to show all entries from my mysql database that have a timestamp earlier than the current month. The entries are in the following format (where the first number is the year, the second is the month, the third is the day):

 

2010-07-23

 

Basically, I want to show all entries that have:

 

2010-06-XX

2010-05-XX

2010-04-XX

2010-03-XX

2010-02-XX

2010-01-XX

 

What would the syntax of the SQL query for something like that look like? What PHP code would I need to grab this? I already have code grabbing the current month, and searching for anything that looks like:

 

'%-$current_month-%'

 

Like I said, please excuse me if this is a rather strange way of going about this.

 

Thanks for any help.

 

 

EDIT: I am currently looking at http://php.net/manual/en/function.date.php to see if there is anything helpful here but I haven't found anything quite suited to my situation...

Link to comment
Share on other sites

So the field in question is a mysql TIMESTAMP column?  You can construct what you need using the MySQL

 

Read my ancient blog entry on this topic:  http://www.gizmola.com/blog/blog/archives/51-Exploring-Mysql-CURDATE-and-NOW.-The-same-but-different..html so you understand the underlying principles of why this works.

 

Let's say your timestamp column is named 'createdDate'

 

SELECT * from yourTbl WHERE createdDate 

Link to comment
Share on other sites

It is a timestamp column actually. Will it work if all entries are followed by 00:00:00? how about if the field is NULL for some entries?

 

I think that might work for me, going to test it out... Thanks for the speedy reply!

 

EDIT:

The fields are actually varchar, go figure.. Trying to convert them over right now but I'm getting an error:

 

#1292 - Incorrect datetime value: '' for column 'columnname' at row 7

 

looks like it doesn't like an empty value somewhere?..

Link to comment
Share on other sites

Nulls, might not work, you will have to test, but you can also add in an AND ISNULL for those if they should be included.  If there is a date, but the time element is all 00:00:00 that's fine.  Again, read my blog entry to understand why. 

Link to comment
Share on other sites

I do not want the NULLs included in what is grabbed from the database, so that is fine. I don't think I am going to be able to convert the structure of the field over in its current state though, can't figure out why. All values are either a timestamp format (2010-07-23 00:00:00) or 'NULL...'

Link to comment
Share on other sites

Yeah looks like something is wrong with that date, so it can't convert it.  You may instead have to convert the values using PHP and its date handling functions, although this will be very poor performance wise, because you can't use mysql's indexes and just get back the rows you want.  You're best off if you can clean the data and insure from now on that it's a valid DATE or DATETIME or TIMESTAMP column.

Link to comment
Share on other sites

Alright, I have found where the problem lies in converting the tables over. There are actually a few dirty entries among the 2000 or so rows. A few of them lack the 00:00:00, and a few were blank, and not NULL.

 

Is there a method for cleaning off the 00:00:00 from all of the rows so I can just fix the blank ones and convert it to just a date?

Link to comment
Share on other sites

Sure, use UPDATE statements to clean up the ones that are garbage.  You can use SQL to find the offenders and fix them prior to conversion.  Look at the mysql string handling functions.

 

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substr

 

Functions like substr and locate should probably be enough to do the job.

Link to comment
Share on other sites

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.