2 Column Mysql Date Range Search in PHP(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..
请帮帮我..
非常感谢..
推荐答案
假设你的 sdate 和 edate 是 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.
如果您的 sdate 和 edate 列不是 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日期范围搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP中的2列Mysql日期范围搜索
基础教程推荐
- Web 服务器如何处理请求? 2021-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- php中的PDF导出 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- php中的foreach复选框POST 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
