假设我的搜索页面上有两个下拉列表和一个按钮:
From
<select id="1stdate">
Until
<select id="2nddate">
<input type="button" id="search">
我想从1stdate
搜索数据直到2nddate
,如何在这种情况下使用WHERE CLAUSE?对于前我想搜索“从09-2010到11-2010”的数据。
我的查询:
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
答案 0 :(得分:1)
我通常会这样做:
<?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'";
请注意:为简单起见,我不添加表单验证。
答案 1 :(得分:0)
这就是你需要的:
WHERE Inspection_datetime BETWEEN $1stdate AND $2nddate
所以它会导致
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