使用WHERE CLAUSE搜索从A日期到B日期的数据 [英] use WHERE CLAUSE for search data from A date until B date

查看:72
本文介绍了使用WHERE CLAUSE搜索从A日期到B日期的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我的搜索页面上有两个下拉列表和一个按钮:

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">

我要搜索从1stdate2nddate的数据,在这种情况下如何使用WHERE CLAUSE?对于前.我想搜索从2010年9月至2010年11月"的数据. 这是我的查询:

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

推荐答案

我通常会执行以下操作:

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.

这篇关于使用WHERE CLAUSE搜索从A日期到B日期的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆