首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从其字段为datetime数据类型的数据库中查找数据

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

Stack Overflow用户
提问于 2014-04-09 05:11:34
回答 1查看 26关注 0票数 0

我使用了下面的代码来获取具有数据类型日期时间的字段的数据,但是它以空集的形式执行结果。请帮我解决这个问题。

代码语言:javascript
复制
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;
}

created_at字段具有日期时间数据类型。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-09 05:20:16

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

通过使用PHP变量并连接:

代码语言:javascript
复制
$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");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22953289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档