dsqbh42082 2018-11-24 05:14
浏览 92
已采纳

带有变量的MySQL UNIX_TIMESTAMP?

Is there any way to pass a condition into UNIX_TIMESTAMP()?

eg : UNIX_TIMESTAMP(-24hr) <- this doesn't work

This returns all records that are newer than the current unix timestamp :

$sql = 'SELECT * FROM events WHERE event_date > UNIX_TIMESTAMP() ORDER BY event_date ASC';

But I would like to return all records that are newer than the current unix timestamp - 24hrs to allow for timezone differences.

Can it be done with the UNIX_TIMESTAMP() function alone?

  • 写回答

1条回答 默认 最新

  • doupian9490 2018-11-24 05:19
    关注

    You can try below

    SELECT * FROM events 
     WHERE event_date > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 24 Hour)) ORDER BY event_date ASC
    

    OR use shorthand version as @Madhur Bhaiya suggested

     SELECT * FROM events 
         WHERE event_date > UNIX_TIMESTAMP(NOW()- INTERVAL 24 Hour) ORDER BY event_date ASC
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发