Php Mysql 在两个日期之间搜索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15438230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Php Mysql Search Between Two Dates
提问by Babu Ahmed
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 usernamewhere 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. why it's doesn't show? Any idea?
但它只是打印用户名 = 18,它应该是打印 18 和 19 号用户名。为什么它不显示?任何的想法?
回答by Treps
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>';
}
回答by Angel
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'
回答by Farhadix
SELECT username FROM oc_calendar WHERE start_date between '$from' AND '$to'

