PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > MySQL problem: date calculation
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
MySQL problem: date calculation

Réponse
 
LinkBack Outils de la discussion
Vieux 04/01/2008, 17h10   #1
plenty900@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 04/01/2008, 17h24   #2
Taliesin Nuin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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).
  Réponse avec citation
Vieux 04/01/2008, 17h37   #3
lark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 04/01/2008, 17h45   #4
Luuk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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




  Réponse avec citation
Vieux 04/01/2008, 18h08   #5
plenty900@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 04/01/2008, 18h12   #6
plenty900@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 04/01/2008, 18h17   #7
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 04/01/2008, 18h19   #8
plenty900@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: MySQL problem: date calculation


> you can also use interval.


Thanks, that worked.
  Réponse avec citation
Vieux 04/01/2008, 18h21   #9
Luuk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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)



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h09.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13311 seconds with 17 queries