views:

30

answers:

2

lets say i have two drop down list and one button on my search page:

From
<select id="1stdate">
Until
<select id="2nddate">
<input type="button" id="search">

i want to search data from 1stdate until 2nddate, how to use WHERE CLAUSE for this case? for ex. i want to search data "from 09-2010 until 11-2010". this my query:

SELECT CONCAT( YEAR(Inspection_datetime ),'-',LPAD(MONTH(Inspection_datetime),2,'0'))
FROM `inspection_report`
GROUP BY  CONCAT( MONTH(Inspection_datetime ),YEAR(Inspection_datetime))
ORDER BY  CONCAT( MONTH(Inspection_datetime ),YEAR(Inspection_datetime))  DESC
A: 

this is what you need:

WHERE Inspection_datetime BETWEEN $1stdate AND $2nddate

so it will result in

SELECT CONCAT( YEAR(Inspection_datetime ),'-',LPAD(MONTH(Inspection_datetime),2,'0'))
FROM `inspection_report` WHERE Inspection_datetime BETWEEN $1stdate AND $2nddate
GROUP BY  CONCAT( MONTH(Inspection_datetime ),YEAR(Inspection_datetime))
ORDER BY  CONCAT( MONTH(Inspection_datetime ),YEAR(Inspection_datetime))  DESC
ITroubs
owh..i guest i couldn't use Inspection_datetime directly.could i use a concat like '".$1stDate."'?
klox
why not? what's the problem with that?
ITroubs
shure you could but writing string slike "This is a string with the value of a var $var in it" is prefectly right because a "" string will be interpreted differently than a '' string and $var will be evaluated
ITroubs
+1  A: 

I usually did something like this:

<?php

list($m1, $y1) = explode($_1stdate);
list($m2, $y2) = explode($_2nddate);

$date1 = "$y1-$m1-01";
$date2 = "$y2-$m2-" . date("t", mktime(0,0,0,$m2, 1, $y2));

$sql = "SELECT *
FROM `inspection_report`
WHERE DATE(Inspection_datetime) BETWEEN '$date1' AND '$date2'";

Please note: for simplicity's sake, I don't add form validations.

silent