如何从Ecto中的两个表中选择数据 [英] How to select data from two tables in Ecto

查看:64
本文介绍了如何从Ecto中的两个表中选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写Ecto查询,该查询将同时从两个表中选择数据。像从table1 t1,table2 t2中选择t1。*,t2。*,其中t1.id = 1和t2.id = 2 我找不到解决方案,只能找到方法编写原始SQL,这看起来并不好。

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]

感谢任何想法

推荐答案

尝试从Ecto.Query

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}

这篇关于如何从Ecto中的两个表中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆