如何解决Ecto WHERE IN 子查询
这是我的子查询(由于 where 子句,它总是返回一条记录):
subset =
from(a in "table1",where: a. id == ^parameter,select: [a.special_id1,a.special_id2,a.special_id3])
这是我的主要查询:
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_id1,special_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])
这应该会为您提供您正在寻找的查询。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。