简体   繁体   English

如何在两个选定日期之间显示数据库中的数据

[英]How to display data from database between two selected date

$result=mysql_query("SELECT * FROM attendance WHERE date Between '$start_date' and '$end_date' order by date asc") or die(mysql_query);

Above mysql query is to get all data in the database between two dates selected by the user. 上面的mysql查询是获取用户选择的两个日期之间数据库中的所有数据。 Let say table attendance in my database as shown below 假设我数据库中的表出席情况如下所示

2009-07-01    3
2009-07-03    4
2009-07-04    5
2009-07-06    7
2009-07-07    6
2009-07-10    8
  1. if the $start_date = "2009-07-01" and $end_date = "2009-07-10", it will display all the data between two selected dates 如果$ start_date =“ 2009-07-01”和$ end_date =“ 2009-07-10”,它将显示两个选定日期之间的所有数据

     2009-07-01 3 2009-07-03 4 2009-07-04 5 2009-07-06 7 2009-07-07 6 2009-07-10 8 

( I noticed that this is successful because, both dates are exist in the database). (我注意到这是成功的,因为两个日期都存在于数据库中)。

  1. if the $start_date = "2009-07-02" and $end_date = "2009-07-09", what i expected it to be displayed is 如果$ start_date =“ 2009-07-02”和$ end_date =“ 2009-07-09”,我希望显示的是

     2009-07-03 4 2009-07-04 5 2009-07-06 7 2009-07-07 6 

Unfortunately, the output never goes out like what I've expected. 不幸的是,输出从未像我期望的那样输出。 This is happened because both dates doesn't exist in the database. 发生这种情况是因为数据库中不存在两个日期。 I know that something wrong with my query. 我知道查询有问题。 It would be great if someone can help me to figure it out. 如果有人可以帮助我解决问题,那就太好了。 Thanks in advanced! 提前致谢!

$result=mysql_query("SELECT * FROM attendance WHERE DATE(date) Between '$start_date' and '$end_date' order by date asc") or die(mysql_query);

要么

$result=mysql_query("SELECT * FROM attendance WHERE DATE(date) >='$start_date' and DATE(date) <='$end_date' order by date asc") or die(mysql_query);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM