Page 1 of 1

select * from table where date('2007-04-18') between field1

Posted: Mon Apr 23, 2007 1:33 am
by m_ramanaji
I wanted php script for the following MySQL code:

mysql>select price from table where date("2007-04-18') between field1 and filed2;

If I give 2007-04-18 and submit, it should show results of that.

id price field1 field2

1 1200 2007-04-01 2007-04-11
2 1500 2007-04-12 2007-04-30

It should results of 1500


I required necessary script for above.

Posted: Mon Apr 23, 2007 10:37 am
by pickle
Just so I'm clear - you want to return all entries in which field1 is before the passed date & field2 is after the passed date?

Posted: Tue Apr 24, 2007 9:28 am
by printf
Something like...

Code: Select all

<?php
$date = '2007-04-18';

$query = "SELECT price FROM table WHERE field_1 <= '" . mysql_real_escape_string ( $date ) . "' AND field_2 >= '" . mysql_real_escape_string ( $date ) . "';";

?>

Posted: Tue Apr 24, 2007 7:43 pm
by califdon
printf gave you the correct syntax. The issue is that the BETWEEN operator applies to a particular field (ONE field, that is) and tests whether that field's value is between two values. It doesn't look at TWO fields.