douhuijun3776 2011-03-11 19:47
浏览 41
已采纳

Mysql FROM_UNIXTIME

I have 2 PHP variables formatted for DATETIME, but the column is in UNIX timestamp. How do I correctly do a BETWEEN on them?

$date_from and $date_to are in DATETIME format. dateline is stored as a UNIX timestamp.

The following isn't working:

if($date_from != "" && $date_to != "") {
    $result_pag_num = mysql_query("SELECT COUNT(*) AS count FROM messages WHERE (folder = '$folder' AND username = '$username' AND FROM_UNIXTIME(dateline) BETWEEN '$date_from' AND '$date_to')");
} else {
    $result_pag_num = mysql_query("SELECT COUNT(*) AS count FROM messages WHERE (folder = '$folder' AND username = '$username')");
}

Edit: all answers were good. I accepted the person who replied the fasted. The error was my own fault.

  • 写回答

3条回答 默认 最新

  • donglieshe4692 2011-03-11 19:51
    关注

    You may want to change the query to

    ... AND dateline BETWEEN UNIX_TIMESTAMP('$date_from') AND UNIX_TIMESTAMP('$date_to')
    

    This way MySQL can use an index on the dateline field. You version forced it to convert every dateline field to a datetime value before doing the comparison, making index usage impossible.

    Otherwise, the query looks fine. Are you sure the $date_from and $date_to are properly formatted, and there's records that fit all the clauses in the query?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?