如何在Ecto查询中使用列值?

时间:2017-07-16 09:24:43

标签: elixir ecto

我有下表:

                                       Table "public.cards"
    Column     |            Type             |                     Modifiers
---------------+-----------------------------+----------------------------------------------------
 id            | integer                     | not null default nextval('cards_id_seq'::regclass)
 question      | text                        | not null
 answer        | text                        | not null
 grade         | integer                     |
 last_trained  | timestamp without time zone |
 next_train    | integer                     | not null
 times_trained | integer                     | not null
 url           | character varying(255)      |
 user_id       | integer                     |
 inserted_at   | timestamp without time zone | not null
 updated_at    | timestamp without time zone | not null

我尝试在Ecto中构建一个与此对应的查询:

SELECT * FROM cards
WHERE date_part('day', now()-last_trained) > next_train;

1 个答案:

答案 0 :(得分:2)

使用Ecto.Query.API.fragment/1

import Ecto.Query

...
from(c in Card, where: fragment("date_part('day', now()-last_trained) > next_train")