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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器