在两个日期之间搜索 Php Mysql

5

我有一个类似这样的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

我正在尝试使用以下查询语句获取在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']; 

但它仅打印了username = 18,应该打印18和19号码的用户名。为什么它没有显示?有什么想法吗?

3个回答

8

查询:

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

你需要一个while循环来显示多个用户名和正确的SQL查询(参见上文):

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

1
SELECT username FROM oc_calendar WHERE start_date between '$from' AND '$to'

0
您可以使用以下查询语句:
$search = mysql_query("SELECT username FROM oc_calendar WHERE start_date between '$from' AND '$to'"); 

关于您正在使用的条件:
where start_date >'$from'

你的fromdate = 2013-03-31,因此上述条件对于用户名为19的用户不成立。相反,你应该使用

where start_date >= '$from' and startdate <='$to'

你的更新查询没有显示出18和19号用户名。 - Babu Ahmed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接