Jump to content

mySql - Date_Sub using all variables


redbrad0

Recommended Posts

Is it possible for the Date_Sub to grab the variables all from the database? I seem to be getting errors with my query and just not sure if its even possible.

 

WHERE
SignupDate < Date_Sub(now() interval numericinterval intervalvalue)

 

In the database the following are the values

numericinterval = 4

intervalvalue = day

Link to comment
Share on other sites

Query I am trying to run, but if I replace _Test.Test_IntervalType with minute then it runs fine.

 

select
*
from
_TestUser,
_TestEmail,
_Test
where
TestUser_TestID=Test_ID
and
TestEmail_TestID=Test_ID
and
TestUser_SignUpDate < Date_Sub(now(), interval _TestEmail.TestEmail_EmailInterval [b]_Test.Test_IntervalType[/b])

 

Here is the dump of the test tables in using this.

CREATE TABLE `_Test` (
  `Test_ID` int(11) NOT NULL auto_increment,
  `Test_Name` varchar(100) default NULL,
  `Test_IntervalType` enum('minute','hour') default 'hour',
  PRIMARY KEY  (`Test_ID`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
insert  into `_Test`(`Test_ID`,`Test_Name`,`Test_IntervalType`) values (1,'Test Name','hour');

CREATE TABLE `_TestEmail` (
  `TestEmail_ID` int(11) NOT NULL auto_increment,
  `TestEmail_TestID` int(11) default NULL,
  `TestEmail_EmailName` varchar(100) default NULL,
  `TestEmail_EmailInterval` int(11) default '0',
  PRIMARY KEY  (`TestEmail_ID`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

insert  into `_TestEmail`(`TestEmail_ID`,`TestEmail_TestID`,`TestEmail_EmailName`,`TestEmail_EmailInterval`) values (1,1,'Test Email',4);

CREATE TABLE `_TestUser` (
  `TestUser_ID` int(11) NOT NULL auto_increment,
  `TestUser_TestID` int(11) default NULL,
  `TestUser_Name` varchar(50) default NULL,
  `TestUser_SignUpDate` datetime default NULL,
  PRIMARY KEY  (`TestUser_ID`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

insert  into `_TestUser`(`TestUser_ID`,`TestUser_TestID`,`TestUser_Name`,`TestUser_SignUpDate`) values (1,1,'Brad','2008-08-16 14:14:14');

Link to comment
Share on other sites

You can do a separate query first to retrieve your numericinterval and intervalvalue values and use those to form a new SQL query.

 

$sql = sprintf("...WHERE SignupDate < DATE_SUB(NOW() interval %0.0f %s)", $numericinterval, $intervalvalue); // Vars retrieved from previous query

Link to comment
Share on other sites

The best is not to use DATE_SUB() in the 'WHERE' clause at all but an actual date. Execute the query to retrieve your values and use PHP strtotime() to calculate the date, then use that in your 'WHERE' query instead of DATE_SUB(). That will be the fastest.

Link to comment
Share on other sites

The best is not to use DATE_SUB() in the 'WHERE' clause at all but an actual date. Execute the query to retrieve your values and use PHP strtotime() to calculate the date, then use that in your 'WHERE' query instead of DATE_SUB(). That will be the fastest.

But that precludes using DB date values....

Link to comment
Share on other sites

Specifying a date/time in the "WHERE" clause will be faster than MySQL executing a function (DATE_SUB) every time.

 

FYI - extra info: Even if a column like SignupDate had a key/index MySQL may or likely would not use the index if used in a function (i.e. DATE_SUB(SignupDate...)).

 

Link to comment
Share on other sites

Specifying a date/time in the "WHERE" clause will be faster than MySQL executing a function (DATE_SUB) every time.

 

FYI - extra info: Even if a column like SignupDate had a key/index MySQL may or likely would not use the index if used in a function (i.e. DATE_SUB(SignupDate...)).

 

Yes, but php takes time to calculate the date too...

 

And if you do the math on the value, not the column, the index can still be used.

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.