Jump to content

Mysql date time stuff


derrick24

Recommended Posts

I'm trying to find out how many updates where done during the month of April, thing is I'm also getting 2007 entries and I don't think this is correct.  >:(

 

SELECT * FROM ads WHERE (flag = 0) AND (updated > 2008-03-29 AND updated < 2008-05-01) order by updated

Link to comment
Share on other sites

CREATE TABLE  `tgv`.`gv_ads` (
  `id` int(10) NOT NULL auto_increment,
  `flag` int(1) default '0',
  `counter` int(10) default '0',
  `cat` varchar(255) default NULL,
  `title` varchar(255) default NULL,
  `address` varchar(255) default NULL,
  `tel` varchar(45) default NULL,
  `fax` varchar(45) default NULL,
  `cell` varchar(45) default NULL,
  `email` varchar(100) default NULL,
  `url` varchar(255) default NULL,
  `description` text,
  `image` varchar(255) default NULL,
  `error` int(11) unsigned default '0',
  `area` varchar(45) default NULL,
  `updated` date default NULL,
  `tel_a` varchar(45) default NULL,
  `tel_b` varchar(45) default NULL,
  `tel_c` varchar(45) default NULL,
  PRIMARY KEY  (`id`),
  KEY `id` (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4745 DEFAULT CHARSET=latin1;

 

Hope that helps, and Ive attached an extract from the database.

 

[attachment deleted by admin]

Link to comment
Share on other sites

I created a test table and the posted query actually returned no rows for me and the problem is you need to put the date values inside of single quotes -

 

SELECT * FROM ads WHERE (flag = 0) AND (updated > '2008-03-29' AND updated < '2008-05-01') order by updated

 

Without the quotes, mysql is apparently performing subtraction: 2008 - 03 - 29 = 1976 and 2008 - 05 - 01 = 2002, resulting in some indeterminate comparison (updated > 1976 AND updated < 2002)

Link to comment
Share on other sites

When selecting a specific month, to keep from needing to calculate the range of dates, I generally use something like the following -

 

SELECT * FROM ads WHERE (flag = 0) AND MONTH(updated) = 4 AND YEAR(updated) = YEAR(CURDATE()) order by updated

 

You could also do the following if you wanted to list more than one specific month -

 

SELECT * FROM ads WHERE (flag = 0) AND MONTH(updated) IN(4,5,6) AND YEAR(updated) = YEAR(CURDATE()) order by updated

 

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.