selecting date records

selecting date records

am 13.03.2006 01:49:37 von WindAndWaves

What is the most efficient way to select records based on a field called NOW
which contains a date or a datetimestamp

I use:
SELECT * FROM `TBL` WHERE
DATE_ADD( `TBL`.`NOW` , INTERVAL 3 MONTH ) > CURDATE( );

Is this the best way?

TIA

> Nicolaas

Re: selecting date records

am 13.03.2006 02:17:37 von gordonb.1a7uv

>What is the most efficient way to select records based on a field called NOW
>which contains a date or a datetimestamp
>
>I use:
>SELECT * FROM `TBL` WHERE
>DATE_ADD( `TBL`.`NOW` , INTERVAL 3 MONTH ) > CURDATE( );
>
>Is this the best way?

I think something like:

SELECT * FROM `TBL` WHERE
`TBL`.`NOW` > DATE_SUB(CURDATE(), INTERVAL 3 MONTH);

might be faster if the query optimizer doesn't deal with it. With
the first query, you evaluate DATE_ADD() once for each row in the
table. With the second, you evaluate DATE_SUB() once. In general,
comparing a table field against a computable run-time constant is
more likely to use an index than comparing some function of a table
field against something. Or, they might be equivalent in speed.

Gordon L. Burditt

Re: selecting date records

am 13.03.2006 03:28:53 von WindAndWaves

Gordon Burditt wrote:
>> What is the most efficient way to select records based on a field
>> called NOW which contains a date or a datetimestamp
>>
>> I use:
>> SELECT * FROM `TBL` WHERE
>> DATE_ADD( `TBL`.`NOW` , INTERVAL 3 MONTH ) > CURDATE( );
>>
>> Is this the best way?
>
> I think something like:
>
> SELECT * FROM `TBL` WHERE
> `TBL`.`NOW` > DATE_SUB(CURDATE(), INTERVAL 3 MONTH);


Thank you. That makes a lot of sense.

> Nicolaas