Implementing date_sub() function with intervals with jOOQ

☆樱花仙子☆ 提交于 2019-12-12 12:08:32

问题


I've been dealing with this since yestertay.

The thing is I am migrating my queries to jOOQ and I got stuck when I tried to implement this part:

select * from table where condition1 and date1 >= date_sub(now(), interval 1 days)

Specifically this part of the condition: date_sub(now(), interval 1 days) with jOOQ.

So my questions are:

  1. Which functions should I use from jOOQ to represent date_sub?

  2. How do I implement interval X days with jOOQ?

To clarify, the dates are of the type Timestamp

Thanks in advance!


回答1:


The solution:

Being slightly biased towards the Oracle database, jOOQ implements adding / subtracting day intervals simply by using:

// Java
DSL.currentTimestamp().sub(1);

The above renders:

-- Oracle
sysdate - 1

-- MySQL
date_add(current_timestamp(), interval -1 day)

Of course, you can also access the date_add() function directly, if you prefer that:

// Java
DSL.dateAdd(DSL.currentTimestamp(), -1);

Some documentation:

  • The manual's section about date time arithmetic
  • The manual's section about interval data types
  • Field.add() Javadoc
  • DSL.dateAdd() Javadoc


来源:https://stackoverflow.com/questions/18386172/implementing-date-sub-function-with-intervals-with-jooq

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!