从字段为datetime数据类型的数据库中查找数据


finding the data from database whose field is in datetime datatype

我使用了以下代码来获取数据类型为datetime的字段的数据,但它以空集形式执行结果。请帮我解决它。

mysql_query("set @last_day=last_day(now()-interval 1 month),@first_day=(@last_day-interval 1 month)+interval 1 day");
$result=mysql_query("select * from `gcm_users` where (`created_at` between @first_day and @last_day)") or die("could not fire the query");
while($row=mysql_fetch_assoc($result))
{
    echo $row['created_at'];
    exit;
}

createdat字段的数据类型为datetime。

不能做两个这样的查询,一个用来设置变量,另一个用来使用变量。这不是一个存储过程。

通过使用PHP变量和串联:

$last_day = "last_day(now()-interval 1 month)";
$first_day = "({$last_day}-interval 1 month)+interval 1 day)";
$sql = "select * from `gcm_users` where (`created_at` between {$first_day} and {$last_day})"
$result = mysql_query($sql) or die("could not fire the query");