Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何根据条件从db获取时间_Mysql_Sql - Fatal编程技术网

Mysql 如何根据条件从db获取时间

Mysql 如何根据条件从db获取时间,mysql,sql,Mysql,Sql,我有一个表,其中的值是 Table_hello date col2 2012-01-31 23:01:01 a 2012-06-2 12:01:01 b 2012-06-3 20:01:01 c 现在我想选择日期 如果是3天前或更短时间,则以天为单位 如果是24小时之前或更短时间,则以小时为单位 如果是60分钟之前或更短时间,则以分钟为单位

我有一个表,其中的值是

             Table_hello
date                            col2
2012-01-31 23:01:01             a
2012-06-2  12:01:01             b
2012-06-3  20:01:01             c
现在我想选择日期

  • 如果是3天前或更短时间,则以天为单位
  • 如果是24小时之前或更短时间,则以小时为单位
  • 如果是60分钟之前或更短时间,则以分钟为单位
  • 以秒为单位,如果是60秒或更短
  • 如果在3天或更长时间之前,以简单格式
  • 输出

    for row1 2012-01-31 23:01:01 
    for row2 1 day ago 
    for row3 1 hour ago 
    
    更新 我的sql查询

      select case 
                when TIMESTAMPDIFF(SECOND, `date`,current_timestamp) <= 60 
                then concat(TIMESTAMPDIFF(SECOND, `date`,current_timestamp), ' seconds')
                when TIMESTAMPDIFF(DAY, `date`,current_timestamp) <= 3 
                then concat(TIMESTAMPDIFF(DAY, `date`,current_timestamp), ' days')end
                when TIMESTAMPDIFF(HOUR, `date`,current_timestamp) <= 60 
                then concat(TIMESTAMPDIFF(HOUR, `date`,current_timestamp), ' hours')
                when TIMESTAMPDIFF(MINUTE, `date`,current_timestamp) <= 60 
                then concat(TIMESTAMPDIFF(MINUTE, `date`,current_timestamp), ' minutes')
    
    from table_hello
    
    选择案例
    当TIMESTAMPDIFF(第二个,`date`,current_timestamp)使用该函数,并使用
    CASE

    select case 
            when TIMESTAMPDIFF(SECOND, `date`,current_timestamp) <= 60 
            then concat(TIMESTAMPDIFF(SECOND, `date`,current_timestamp), ' seconds')
            when TIMESTAMPDIFF(DAY, `date`,current_timestamp) <= 3 
            and TIMESTAMPDIFF(DAY, `date`,current_timestamp) >= 1 
            ...
           end as time_diff 
    from Table_hello
    where TIMESTAMPDIFF(DAY, `time`,current_timestamp) >= 3 
    
    选择案例
    当TIMESTAMPDIFF(秒,`date`,当前时间戳)=3时
    
    使用该功能和
    案例

    select case 
            when TIMESTAMPDIFF(SECOND, `date`,current_timestamp) <= 60 
            then concat(TIMESTAMPDIFF(SECOND, `date`,current_timestamp), ' seconds')
            when TIMESTAMPDIFF(DAY, `date`,current_timestamp) <= 3 
            and TIMESTAMPDIFF(DAY, `date`,current_timestamp) >= 1 
            ...
           end as time_diff 
    from Table_hello
    where TIMESTAMPDIFF(DAY, `time`,current_timestamp) >= 3 
    
    选择案例
    当TIMESTAMPDIFF(秒,`date`,当前时间戳)=3时
    
    能否提供一些示例输出?像上面的例子一样,您是否希望它输出75天、24分钟、10秒作为行值?你的意思是“简单格式”简单格式是按存储的方式显示日期如果存储为int而不是datetime,这会很容易吗?Mysql内置了获取时差的函数。另外,您是否考虑过在应用程序级别而不是在数据库中进行此比较?能否提供一些示例输出?像上面的例子一样,您是否希望它输出75天、24分钟、10秒作为行值?你的意思是“简单格式”简单格式是按存储的方式显示日期如果存储为int而不是datetime,这会很容易吗?Mysql内置了获取时差的函数。另外,您是否考虑过在应用程序级别而不是在数据库中进行这种比较?很好,但是使用5个案例将加快我的查询速度,或者可能会有一点。测试它,看看它是否对您有问题。顺便问一下,我如何知道时间是以天而不是以小时或秒存储时间的?您可以使用
    concat
    。查看我的udpdate。我正确理解您的意思,然后您不需要表中的所有记录。然后使用
    where
    子句对其进行限制。查看我的更新。这就是你想要的吗?很好,但是使用5个案例将加快我的查询速度,或者可能会有一点。测试它,看看它是否对您有问题。顺便问一下,我如何知道时间是以天而不是以小时或秒存储时间的?您可以使用
    concat
    。查看我的udpdate。我正确理解您的意思,然后您不需要表中的所有记录。然后使用
    where
    子句对其进行限制。查看我的更新。这就是你想要的吗?