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日期范围搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)