【问题标题】:How to select data from two tables in Ecto如何从 Ecto 中的两个表中选择数据
【发布时间】:2016-07-14 09:56:18
【问题描述】:

我尝试编写 Ecto 查询,该查询将同时从两个表中选择数据。像Select t1.*,t2.* from table1 t1,table2 t2 where t1.id=1 and t2.id=2 我找不到解决方案,找到了编写原始 SQL 的唯一方法,看起来不太好。

类似于变体 - 使用预加载,但它会产生额外的查询。

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

感谢您的任何想法

【问题讨论】:

    标签: elixir ecto


    【解决方案1】:

    从 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}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 2011-06-21
      • 1970-01-01
      • 2012-11-18
      • 2012-12-16
      • 2012-09-13
      • 1970-01-01
      相关资源
      最近更新 更多