PHP中的2列Mysql日期范围搜索

2 Column Mysql Date Range Search in PHP(PHP中的2列Mysql日期范围搜索)
本文介绍了PHP中的2列Mysql日期范围搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

MySQL 列 > sdate、edate(它的第 2 列).

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

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

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

假设用户输入了 sdate(例如,2008 年 12 月 22 日)和 edate(例如,2008 年 12 月 30 日).

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

它应该显示

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

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

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

请帮帮我..

非常感谢..

推荐答案

假设你的 sdateedate 是 MySQL 列类型 DATE您可以执行以下操作:

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'

或者你可以使用 DATEDIFF

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

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

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.

如果您的 sdateedate 列不是 DATE 列,那么您就不走运了,需要先更改它们.

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Difference between mt_rand() and rand()(mt_rand() 和 rand() 的区别)
Coalesce function for PHP?(PHP的合并函数?)
Fastest way to convert string to integer in PHP(在 PHP 中将字符串转换为整数的最快方法)
mysql match against ~ example(mysql 匹配 ~ 示例)
How-to: Ranking Search Results(操作方法:对搜索结果进行排名)
How to generate the snippet like those generated by Google with PHP and MySQL?(如何生成类似于 Google 使用 PHP 和 MySQL 生成的代码段?)