tags:

views:

138

answers:

2

Hi, I need some help figuring out and SQL Statement.

I know what I want I just cant express it.

Im using php, so it doesnt need to be exclusivly SQL, its to act as a filter.

Pseudo code

$query="SELECT * FROM MyTable WHERE 'TIS'  is not older than 2 days or empty  = ''$ORDER"; }

TIS in the name of the column in my table were I store dates in this format 03-12-09 (d,m,y). The $ORDER is for ordering the values based on values from other fields not dates.

Im looking at

SELECT * 
FROM orders
WHERE day_of_order >
(SELECT DATEADD(day,-30, (SELECT MAX(day_of_order) FROM orders)) AS "-30 Days");

But i dont quite think im on the rigth track with this.

Thanks

+2  A: 

Try the following:

SELECT *
  FROM MyTable
  WHERE COALESCE(TIS, SYSDATE) > SYSDATE - INTERVAL '2' DAY
  $ORDER

I don't know what database you're using - the above uses Oracle's method of dealing with time intervals. If you're using SQL Server the following should be close:

SELECT *
  FROM MyTable
  WHERE COALESCE(TIS, GETDATE()) > DATEADD(Day, -2, GETDATE())
  $ORDER

In MySQL try this:

SELECT *
  FROM MyTable
  WHERE COALESCE(TIS, NOW()) > DATE_SUB(NOW(), INTERVAL 2 DAYS)
  $ORDER 

I hope this helps.

Bob Jarvis
Im using MySql, with MAMP.
Marvin
OK, then try SELECT * FROM MyTable WHERE COALESCE(TIS, NOW()) > DATE_SUB(NOW, INTERVAL 2 DAYS) ORDER BY $ORDER
Bob Jarvis
I get nothing. To organize this. - `ORDER BY` isnt necessary as it included in `$ORDER`. - Im still not sure what format for the value is expected to be present oin `TIS`. - What does mean `COALESCE`? Thank you for your help.
Marvin
I tried running my query directy and i got:#1305 - FUNCTION FACT.DATEADD does not exist From what I understand is that im trying to quere the db name instead of the table inside?FACT is my DB name.
Marvin
The error occurs because MySQL does not support the DATEADD function. I've put the MySQL version of the statement into the answer above - give it a try. COALESCE returns the first non-NULL value in its argument list (when the argument list is read from left to right).
Bob Jarvis
Thank you for your explanation. I stll cant make it work, il have to leave at that for now. I apreciate your help.
Marvin
There was a still a minor problem in the MySQL statement (second NOW didn't have parentheses after it). You might try it again.
Bob Jarvis
A: 

So, I was pretty lost in all this.

How did it got solved:

  • First I understood that the Statement I was using was not supported by MySql thanks to eligthment from Bob Jarvis.

_ Second In a comment by vincebowdren wich "strongly" adviced me to change the data type on that field to Date wich indeed I had not, it was a string.

It was pretty Dumb for me to try using SQL operations for Dates on a field that had String values.

So I just RTFM: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

and:

mysql> SELECT something FROM tbl_name -> WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date_col;

Then proceeded to change the field value to date.

and this is my perfectly working query:

$query="SELECT * FROM MyTable WHERE DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= TIS OR TIS = 0000-00-00 $ORDER "; }

I would like to thank the posters for their aid.

Marvin
Ah, hadn't seen this. Glad it works now.
Bob Jarvis