简体   繁体   English

两个日期之间的Php Mysql搜索

[英]Php Mysql Search Between Two Dates

I've mysql table like this: 我有这样的mysql表:

id      start_date         username
1       2013-04-04         18
2       2013-03-31         19
3       2013-04-04         19
4       2013-04-02         19 
5       2013-04-03         18

I'm trying to get username where start_date is between 2013-03-31 to 2013-05-01 with following query: 我正在尝试使用以下查询获取start_date介于2013-03-31至2013-05-01之间的用户名

// $from = 2013-03-31 and $to = 2013-03-01 (example)

$search = mysql_query("SELECT username FROM oc_calendar WHERE start_date >'$from' AND 
start_date < '$to'"); 
$re_search = mysql_fetch_array($search);
echo $search_p_id = $re_search['username']; 

But It's just print username = 18, It's should be print 18 and 19 number username. 但它只是打印用户名= 18,它应该是打印18和19号用户名。 why it's doesn't show? 为什么它不显示? Any idea? 任何的想法?

Query: 查询:

$search = mysql_query("SELECT username FROM oc_calendar WHERE 
start_date between '$from' AND '$to'");

And you need a while-loop to display more that one username and a correct SQL-query (see above): 你需要一个while循环来显示更多的用户名和正确的SQL查询(见上文):

while($re_search = mysql_fetch_array($search)) {
  $re_search['username'] . '<br>';
}

You can use this query : 您可以使用此查询:

$search = mysql_query("SELECT username FROM oc_calendar WHERE start_date between '$from' AND '$to'"); 

Regarding the condition that you are using : 关于您使用的条件:

where start_date >'$from'

your fromdate = 2013-03-31 so the above mentioned condition is not true for username = 19. Instead you should use 你的fromdate = 2013-03-31所以上面提到的条件不适用于username = 19.相反你应该使用

where start_date >= '$from' and startdate <='$to'
SELECT username FROM oc_calendar WHERE start_date between '$from' AND '$to'

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

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