2列Mysql日期范围搜索PHP [英] 2 Column Mysql Date Range Search in PHP

查看:122
本文介绍了2列Mysql日期范围搜索PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MySQL列> sdate,edate(其2列)。



sdate是项目开始的开始日期,edate是项目结束的结束日期。

所以我需要在他们之间进行搜索。

 < strong>搜索< ; / strong>< br /> 
< form method =postaction =search.php>
开始报告日期:< input type =textname =sdate/>
结束报告日期:< input type =textname =edate/>
< input type =submitname =Submitvalue =Search/>
< / form>

这是mysql中的示例数据
sdate项目名称edate
2008年12月22日项目1 2008年12月23日
2008年12月25日项目2 2008年12月26日
2008年12月24日项目3 2008年12月24日
2008年1月1日项目4 2008年1月20日
2008年12月10日项目5 2008年12月12日

所以说一个用户输入sdate(例如,2008年12月22日)和edate(例如2008年12月30日)。



应显示

  2008年12月22日项目1 2008年12月23日
2008年12月25日项目2 2008年12月26日
2008年12月24日项目3 2008年12月27日

所以我需要一个php代码sql查询,应该显示条目在这两个日期之间..



请帮助我..



非常感谢..

解决方案

假设哟$ sdate edate 是MySQL列类型 DATE 你可以执行以下操作:

  SELECT 
Project_Name
,sdate
,edate
FROM your_table
WHERE
sdate< ='2008-12-26'
AND
edate> ='2008-12-26'

或者您可以使用DATEDIFF

  SELECT 
Project_Name
,sdate
,edate
FROM your_table
WHERE
DATEDIFF(sdate,'2008-12-26')< = 0
AND
DATEDIFF(edate,'2008-12-26')> = 0

第一个更高效,因为MySQL可以将表中的所有行与静态值进行比较。对于第二个解决方案,它需要计算表中每一行的差异。



如果您的 sdate edate 列不是DATE列,您没有运气,需要先更改。


MySQL column > sdate, edate ( its 2 column).

sdate is start date for project starting and edate is end date for project ending.

so i need to make search between them..

<strong>Search</strong><br />
<form method="post" action="search.php">
  Start Report Date : <input type="text" name="sdate" />
  End Report Date : <input type="text" name="edate" />
  <input type="submit" name="Submit" value="Search" />
</form>

This is example data in mysql
sdate             Project Name      edate
22 December 2008  project 1         23 December 2008
25 December 2008  project 2         26 December 2008
24 December 2008  project 3         27 December 2008
1  January 2008   project 4         20 January 2008
10 December 2008  project 5         12 December 2008

so let say a user entered sdate ( eg, 22 December 2008 ) and edate ( eg, 30 December 2008 ).

It should display

22 December 2008  project 1         23 December 2008
25 December 2008  project 2         26 December 2008
24 December 2008  project 3         27 December 2008

So i need a php code sql query which should display entries lies between those 2 dates..

Please help me..

Thanks very much..

解决方案

assuming that your sdate and edate are of MySQL columns type DATE you could do the following:

SELECT  
  Project_Name
, sdate
, edate
FROM your_table 
WHERE 
  sdate <= '2008-12-26'
 AND 
  edate >= '2008-12-26'

or you could use DATEDIFF

SELECT  
  Project_Name
, sdate
, edate
FROM your_table 
WHERE 
  DATEDIFF(sdate, '2008-12-26') <= 0
 AND 
  DATEDIFF(edate, '2008-12-26') >= 0

The first one is more efficient because MySQL can compare all the rows in your table to a static value. For the second solution it needs to calculate the difference for every row in your table.

If your sdate and edate columns are not DATE columns, you are out of luck and need to change them first.

这篇关于2列Mysql日期范围搜索PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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