How to select data from two tables in Ecto

2019-09-01 04:49发布

I trying to write Ecto query which will be select data from two tables at the same time. Like Select t1.*,t2.* from table1 t1,table2 t2 where t1.id=1 and t2.id=2 I can't find solution, found only way to write raw SQL and it's looks like not good.

Like variant -using preload, but it's spawn additional query.

comments_query = from c in Comment, order_by: c.published_at
Repo.all from p in Post, preload: [comments: ^comments_query]

Thanks for any ideas

标签: elixir ecto
1条回答
在下西门庆
2楼-- · 2019-09-01 04:59

Try this from Ecto.Query

https://hexdocs.pm/ecto/Ecto.Query.html#join/5

from t1 in Table1,
   join: t2 in Table2, on: t1.t2_id == t2.id,
   select: {t1.title, t2.text}
查看更多
登录 后发表回答