在Ecto中使用递归CTE

编程入门 行业动态 更新时间:2024-10-28 16:21:31
本文介绍了在Ecto中使用递归CTE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在计划与Ecto一起运行的查询中使用递归CTE的结果?例如,假设我有一个表,节点,其结构如下:

How would I go about using the result of a recursive CTE in a query I plan to run with Ecto? For example let's say I have a table, nodes, structured as so:

-- nodes table example -- id parent_id 1 NULL 2 1 3 1 4 1 5 2 6 2 7 3 8 5

我还有另外一个表nodes_users,其结构如下:

and I also have another table nodes_users structured as so:

-- nodes_users table example -- node_id user_id 1 1 2 2 3 3 5 4

现在,我想抓住一个节点位于特定节点或特定节点之上的所有用户,为了举例说明,我们选择ID为8的节点.

Now, I want to grab all the users with a node at or above a specific node, for the sake of an example let's choose the node w/ the id 8.

我可以使用以下递归postgresql 查询为此:

I could use the following recursive postgresql query to do so:

WITH RECURSIVE nodes_tree AS ( SELECT * FROM nodes WHERE nodes.id = 8 UNION ALL SELECT n.* FROM nodes n INNER JOIN nodes_tree nt ON nt.parent_id = n.id ) SELECT u.* FROM users u INNER JOIN users_nodes un ON un.user_id = u.id INNER JOIN nodes_tree nt ON nt.id = un.node_id

这应该返回用户.* ID为1、2和4的用户.

This should return users.* for the users w/ id of 1, 2, and 4.

我不确定如何使用ecto运行相同的查询,理想情况下,该查询将返回可链接的输出.我知道我可以使用片段宏将原始SQL插入查询中,但是我不确定这将在哪里使用,或者这是否是最合适的选择.

I'm not sure how I could run this same query using ecto, ideally in a manner that would return a chainable output. I understand that I can insert raw SQL into my query using the fragment macro, but I'm not exactly sure where that would go for this use or if that would even be the most appropriate route to take.

帮助和/或建议将不胜感激!

Help and/or suggestions would be appreciated!

推荐答案

我能够使用片段来完成此任务.这是我使用的代码示例.我可能会将这种方法移到存储过程中.

I was able to accomplish this using a fragment. Here's an example of the code I used. I'll probably move this method to a stored procedure.

Repo.all(MyProj.User, from u in MyProj.User, join: un in MyProj.UserNode, on: u.id == un.user_id, join: nt in fragment(""" ( WITH RECURSIVE node_tree AS ( SELECT * FROM nodes WHERE nodes.id = ? UNION ALL SELECT n.* FROM nodes n INNER JOIN node_tree nt ON nt.parent_id == n.id ) ) SELECT * FROM node_tree """, ^node_id), on: un.node_id == nt.id )

更多推荐

在Ecto中使用递归CTE

本文发布于:2023-10-16 12:36:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497571.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   Ecto   CTE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!