Teo Choong Ping
Teo Choong Ping

Reputation: 12798

How do I translate this SQL to ecto query?

How do I translate this SQL to ecto query

SELECT * 
 FROM table_name
 WHERE table_name.the_date > DATE_SUB(NOW(), INTERVAL 1 DAY)

without using fragment.

This works for me but I am wondering is there is a ecto idiomatic way to do this kind of query.

iex(22)> query = from n in Table,
...(22)> where: fragment("updatedAt > DATE_SUB(now(), INTERVAL 1 DAY)"),
...(22)> select: n;

Upvotes: 1

Views: 224

Answers (1)

NoDisplayName
NoDisplayName

Reputation: 15736

You could rewrite it to this:

from t in Table,
where: t.updatedAt > datetime_add(^Ecto.DateTime.utc, -1, "day")

The docs are here

Upvotes: 4

Related Questions