【问题标题】:SQL "Where" syntax for is todaySQL "Where" 语法是今天
【发布时间】:2013-07-31 12:27:27
【问题描述】:

我需要今天curdate 所在的所有条目... curdate 的类型为 timestamp。 当然我在谷歌搜索过这个,但我认为我使用了错误的关键字。

select * from ftt.element fee, ftt.plan p 
where p.id=ftt.p_id and curdate ??? 
order by p_id, curdate desc;

你能提供给我吗?

【问题讨论】:

  • 这是用 PHP 完成的吗?
  • 您使用的是什么关系型数据库? mysql?后格雷斯?甲骨文?当前日期的函数在每个函数中可能不同。
  • 不,这只是 Oracle 的声明......

标签: sql oracle timestamp


【解决方案1】:

SYSDATE 返回 Oracle 中的当前日期a 时间。 DATE 列还包含 Oracle 中的时间部分 - TIMESTAMP 列也是如此。

为了检查“今天”,您必须删除两个值中的时间部分:

select * 
from ftt.element fee
  join ftt.plan p on p.id=ftt.p_id 
where trunc(curdate) = trunc(sysdate)
order by p_id, curdate desc;

您还应该习惯使用显式的JOINs 而不是WHERE 子句中的隐式连接。

如果您需要处理不同的时区(例如,因为 curdatetimestamp with time zone),那么您可能想要使用 CURRENT_TIMESTAMP(包括时区)而不是 SYSDATE

【讨论】:

  • 正是我正在寻找的!非常感谢
【解决方案2】:

这个答案与 a_horses_with_no_name 的答案非常相似。不同之处在于它执行得更快。在比较之前对列进行计算会降低性能。

select * 
from ftt.element fee
join ftt.plan p on p.id=ftt.p_id 
where curdate >= trunc(sysdate)
and curdate < trunc(sysdate + 1)
order by p_id, curdate desc;   

【讨论】:

  • 您可以随时在trunc(curdate)上创建索引
  • @a_horse_with_no_name 制作索引以弥补不良代码,抱歉说是不好的做法
  • 很抱歉这么说:) - 但我不认为这是“糟糕的代码”。这是一个有效的条件。
  • @a_horse_with_no_name 然后一定要继续做你所做的。最终你会改变主意
  • @a_horse_with_no_name 这是一个有趣的链接Aaron Bertrand's homepage
【解决方案3】:

有基本的日间功能。这里有一对:

getDate()
Now()

在不了解您的 DBMS 的情况下,我不能说哪个特别有效

【讨论】:

    【解决方案4】:

    到目前为止,您所做的一切都非常麻烦!!!!我从上述查询中得出的结论是您正在尝试加入 2 个表。 首先加入2个表:

    Select * from table1 join table2 on table1.id = table2.id;
    

    现在在此查询中包含任何条件,将其连接到上述查询之后:

    Select * from table1 join table2 on table1.id = table2.id where curdate = date;
    

    这里定义 date = CURDATE();在您触发查询之前。 希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      相关资源
      最近更新 更多