简体   繁体   English

MSACCESS:选择“多少天前” {可以返回:今天,昨天,2天前,…},field1,field2,fieldN FROM mytable

[英]MSACCESS: SELECT “How many days ago” {can return: today, yesterday, 2 days ago, …}, field1, field2, fieldN FROM mytable

I have a table with a timestamp column, and want to perform a SELECT that returns this column in a friendly way that express how many days ago it occurred, like "Today", "Yesterday", "2 days ago", "3 days ago"... 我有一个带有timestamp列的表,并且想要执行SELECT,以一种友好的方式返回此列,该列表示发生在几天前,例如“ Today”,“ Yesterday”,“ 2 days ago”,“ 3 days”前”...

I'm unable to process data on destination, so my query must return the final string. 我无法处理目标数据,因此我的查询必须返回最终字符串。

I'm already using DateDiff("d",timestamp,Date()) to determine an integer that represents, but I need to transform to the corresponding string. 我已经在使用DateDiff(“ d”,timestamp,Date())确定一个表示的整数,但是我需要转换为相应的字符串。

I already suceeded constructing a helper table like [ intDays (PK), strDays ] and inner joinning, but this way I need to pre-populate with all range of values needed. 我已经成功构建了一个[intDays(PK),strDays]和内部联接之类的辅助表,但是这种方式下我需要预先填充所需的所有范围的值。 I'm looking for a more universal solution that follows a conditional logic to output the correct string, like: (pseudo-code) 我正在寻找一种更通用的解决方案,该解决方案遵循条件逻辑来输出正确的字符串,例如:(伪代码)

If DateDifference = 0 Then return "Today"
If DateDifference = 1 Then return "Yesterday"
return DateDifference & " days ago"

You can use iif() : 您可以使用iif()

select iif(DateDiff("d", timestamp, Date()) = 0, "Today",
           iif(DateDiff("d", timestamp, Date()) = 1, "Yesterday",
               DateDiff("d", timestamp, Date()) & " days ago"
              )
          )

Note: the last expression might need to be cstr(DateDiff("d", timestamp, Date())) & " days ago" . 注意:最后一个表达式可能需要为cstr(DateDiff("d", timestamp, Date())) & " days ago" I'm not sure if MS Access does the conversion automatically or generates an error if one of the arguments is not a string. 我不确定MS Access是否自动进行转换,或者如果其中一个参数不是字符串,则会生成错误。

This type of logic would often be done at the application level. 这类逻辑通常会在应用程序级别完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM