1
votes

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;
1

1 Answers

4
votes

You could rewrite it to this:

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

The docs are here