Sql 子查询中的位置

Sql 子查询中的位置,sql,subquery,elixir,ecto,Sql,Subquery,Elixir,Ecto,这是我的子查询(由于where子句,它将始终返回一条记录): 这是我的主要问题: from(b in "table2", where: b.id in subquery(subset), select: [:first_name, :last_name] 获取此错误: ** (Ecto.QueryError) subquery must return a single field in order to be used on the right-side of `in` i

这是我的子查询(由于where子句,它将始终返回一条记录):

这是我的主要问题:

from(b in "table2", where: b.id in subquery(subset), select: [:first_name, :last_name]
获取此错误:

** (Ecto.QueryError) subquery must return a single field in order to be used on the right-side of `in` in query:

我理解为什么子查询会返回这样的结构
[[special\u id1,special\u id2…]
。我希望子查询只返回一个平面列表。我怎样才能做到这一点呢?

你不能。但是,您可以将
exists/1
查询与父字段上匹配的子查询一起使用:

subset = 
  from(a in "table1",
    where: a.id == ^parameter,
    where: parent_as(:table2).id in [a.special_id1, a.special_id2, a.special_id3],
    select: 1)

from(b in "table2", as: :table2,
  where: exists(subquery(subset)),
  select: [:first_name, :last_name])
这将为您提供所需的查询

subset = 
  from(a in "table1",
    where: a.id == ^parameter,
    where: parent_as(:table2).id in [a.special_id1, a.special_id2, a.special_id3],
    select: 1)

from(b in "table2", as: :table2,
  where: exists(subquery(subset)),
  select: [:first_name, :last_name])