How to select data from two tables in Ecto

瘦欲@ 提交于 2019-12-13 07:25:55

问题


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


回答1:


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}


来源:https://stackoverflow.com/questions/36240347/how-to-select-data-from-two-tables-in-ecto

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