MySQL problem: date calculation

This is a discussion on MySQL problem: date calculation within the MySQL Database forums, part of the Database Forums category; Hi folks, I'm having a problem with MySQL. I have a table, one of whose columns is called `date`, ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-04-2008
plenty900@yahoo.com
 
Posts: n/a
Default MySQL problem: date calculation

Hi folks,

I'm having a problem with MySQL. I have a table,
one of whose columns is called `date`, which is a timestamp.
I need to SELECT all the rows whose timestamps are
less than 7 days old. To my surprise, all of the examples
of using DATEDIFF from the MySQL online reference are failing.

For instance, this does not work:

SELECT * FROM mytable WHERE 7 > DATEDIFF(`date`,CURDATE());

Nor does this:

SELECT * FROM mytable WHERE 7 > DATEDIFF(`date`, NOW());

Any ideas?
Incidentally I'm accessing the db through php_admin,
and I am not sure how to get the MySQL version number
but it is not displayed anywhere.

Thanks.
Reply With Quote
  #2 (permalink)  
Old 01-04-2008
Taliesin Nuin
 
Posts: n/a
Default Re: MySQL problem: date calculation

plenty900@yahoo.com wrote:
> Hi folks,
>
> I'm having a problem with MySQL. I have a table,
> one of whose columns is called `date`, which is a timestamp.
> I need to SELECT all the rows whose timestamps are
> less than 7 days old. To my surprise, all of the examples
> of using DATEDIFF from the MySQL online reference are failing.
>
> For instance, this does not work:
>
> SELECT * FROM mytable WHERE 7 > DATEDIFF(`date`,CURDATE());
>
> Nor does this:
>
> SELECT * FROM mytable WHERE 7 > DATEDIFF(`date`, NOW());
>
> Any ideas?
> Incidentally I'm accessing the db through php_admin,
> and I am not sure how to get the MySQL version number
> but it is not displayed anywhere.
>
> Thanks.


Don't you want the parameters the other way around? I.e.:
DATEDIFF(NOW(),date_val);

You can find the server version of MySQL in PHPMyadmin on the root page
just under the host name called Server Version. (The client version is
across from it on the right).
Reply With Quote
  #3 (permalink)  
Old 01-04-2008
lark
 
Posts: n/a
Default Re: MySQL problem: date calculation

plenty900@yahoo.com wrote:
> Hi folks,
>
> I'm having a problem with MySQL. I have a table,
> one of whose columns is called `date`, which is a timestamp.
> I need to SELECT all the rows whose timestamps are
> less than 7 days old. To my surprise, all of the examples
> of using DATEDIFF from the MySQL online reference are failing.
>
> For instance, this does not work:
>
> SELECT * FROM mytable WHERE 7 > DATEDIFF(`date`,CURDATE());
>
> Nor does this:
>
> SELECT * FROM mytable WHERE 7 > DATEDIFF(`date`, NOW());
>
> Any ideas?
> Incidentally I'm accessing the db through php_admin,
> and I am not sure how to get the MySQL version number
> but it is not displayed anywhere.
>
> Thanks.


you can also use interval.
Reply With Quote
  #4 (permalink)  
Old 01-04-2008
Luuk
 
Posts: n/a
Default Re: MySQL problem: date calculation


<plenty900@yahoo.com> schreef in bericht
news:4b12fecb-d9b1-4097-b41d-98a65213d81c@i3g2000hsf.googlegroups.com...
> Hi folks,
>
> I'm having a problem with MySQL. I have a table,
> one of whose columns is called `date`, which is a timestamp.
> I need to SELECT all the rows whose timestamps are
> less than 7 days old. To my surprise, all of the examples
> of using DATEDIFF from the MySQL online reference are failing.
>
> For instance, this does not work:
>
> SELECT * FROM mytable WHERE 7 > DATEDIFF(`date`,CURDATE());
>
> Nor does this:
>
> SELECT * FROM mytable WHERE 7 > DATEDIFF(`date`, NOW());
>
> Any ideas?
> Incidentally I'm accessing the db through php_admin,
> and I am not sure how to get the MySQL version number
> but it is not displayed anywhere.
>
> Thanks.


Try:
SELECT *, DATEDIFF(`date`, NOW()) FROM mytable ;

and see for yourself what you're missing....

http://dev.mysql.com/doc/refman/5.0/...ction_datediff




Reply With Quote
  #5 (permalink)  
Old 01-04-2008
plenty900@yahoo.com
 
Posts: n/a
Default Re: MySQL problem: date calculation


> SELECT *, DATEDIFF(`date`, NOW()) FROM mytable ;


I get this:
MySQL said: Documentation
#1064 - You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL server version for the right syntax to use
near '(`mydate`, NOW()) FROM `mytable`' at line 1
Reply With Quote
  #6 (permalink)  
Old 01-04-2008
plenty900@yahoo.com
 
Posts: n/a
Default Re: MySQL problem: date calculation


> http://dev.mysql.com/doc/refman/5.0/...nctions.html#f...


On that page, it suggests typing the following:

SELECT DATEDIFF( '1997-12-31 23:59:59', '1997-12-30' ) ;

Guess what? That gives:

MySQL said: Documentation
#1064 - You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL server version for the right syntax to use
near '('1997-12-31 23:59:59','1997-12-30')' at line 1

Reply With Quote
  #7 (permalink)  
Old 01-04-2008
Rik Wasmus
 
Posts: n/a
Default Re: MySQL problem: date calculation

On Fri, 04 Jan 2008 19:12:09 +0100, <plenty900@yahoo.com> wrote:

>
>> http://dev.mysql.com/doc/refman/5.0/...nctions.html#f...

>
> On that page, it suggests typing the following:
>
> SELECT DATEDIFF( '1997-12-31 23:59:59', '1997-12-30' ) ;
>
> Guess what? That gives:
>
> MySQL said: Documentation
> #1064 - You have an error in your SQL syntax. Check the manual that
> corresponds to your MySQL server version for the right syntax to use
> near '('1997-12-31 23:59:59','1997-12-30')' at line 1


What version of mysql are you using? (DATEDIFF() was introduced in v4.1.1).
--
Rik Wasmus
Reply With Quote
  #8 (permalink)  
Old 01-04-2008
plenty900@yahoo.com
 
Posts: n/a
Default Re: MySQL problem: date calculation


> you can also use interval.


Thanks, that worked.
Reply With Quote
  #9 (permalink)  
Old 01-04-2008
Luuk
 
Posts: n/a
Default Re: MySQL problem: date calculation


<plenty900@yahoo.com> schreef in bericht
news:7fb4b744-c243-4844-9611-37d3877361a3@5g2000hsg.googlegroups.com...
>
>> http://dev.mysql.com/doc/refman/5.0/...nctions.html#f...

>
> On that page, it suggests typing the following:
>
> SELECT DATEDIFF( '1997-12-31 23:59:59', '1997-12-30' ) ;
>
> Guess what? That gives:
>
> MySQL said: Documentation
> #1064 - You have an error in your SQL syntax. Check the manual that
> corresponds to your MySQL server version for the right syntax to use
> near '('1997-12-31 23:59:59','1997-12-30')' at line 1
>


can you do this:
SHOW VARIABLES LIKE '%version%';

to show the version of mysql you are using
(see: http://dev.mysql.com/doc/refman/4.1/...variables.html)



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:40 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0