在什么日期BETWEEN不工作

时间:2014-10-20 08:19:43

标签: php mysql

我试图通过计算自动整数(id)在开始日期和结束日期之间出现的次数来按日期范围计算登录次数。

我从上一页的表格(y-m-d)中获取开始日期和结束日期。

$start_date=$_POST['start_date']; /*in this case its "2014-10-10"*/
$end_date=$_POST['end_date']; /*in this case its "2014-10-20"*/


$sql = <<<SQL
SELECT id, COUNT(*) as login_count FROM `usage`  
GROUP BY id   
WHERE date
BETWEEN $start_date AND $end_date
SQL;

但是我一直收到以下语法错误

&#34;运行查询时出错[您的SQL语法出错;检查与您的MySQL服务器版本相对应的手册,以便使用正确的语法,以便在2014-10-10和2014-10-20之间的约会日期使用&#39;在第3行]&#34;

我做错了什么?

5 个答案:

答案 0 :(得分:1)

你的$ sql应该是:

$sql = <<<SQL
SELECT id, COUNT(*) as login_count FROM `usage`  
WHERE date
BETWEEN $start_date AND $end_date
GROUP BY id   
SQL;

GROUP BY CLAUSE语法为AFTER WHERE

答案 1 :(得分:1)

你需要将'' s放在sql中的日期文字中,如下所示:

$response = mysql_query("SELECT * FROM `db`.`$sql_table` WHERE (date BETWEEN '$end_date' AND '$start_date')  ORDER by id ASC ")or die(mysql_error());

答案 2 :(得分:0)

首先,您应引用日期&#39; $ start_date&#39; AND&#39; $ end_date&#39;并且你一定要确保这些值被转义以避免SQL注入。

答案 3 :(得分:0)

你的SQL有错误。试试这个:

SELECT id, COUNT(*) as login_count FROM `usage`  
WHERE (date BETWEEN $start_date AND $end_date)
GROUP BY id;

答案 4 :(得分:0)

try it 

$sql = <<<SQL
SELECT id, COUNT(*) as login_count FROM `usage`  
WHERE date
BETWEEN '$start_date' AND '$end_date'
GROUP BY id   
SQL;