recursive-query

CONNECT BY or hierarchical queries in RDBMS other than Oracle

情到浓时终转凉″ 提交于 2019-11-28 03:56:07
问题 Oracle ships with a very handy feature. You can create hierarchical queries (recursive behaviour) using the following clause: CONNECT BY [NOCYCLE] {condition [AND condition...]} [START WITH condition] As documented here: http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/queries003.htm I'm wondering, are there any other established RDBMS that support an equivalent or similar syntax? Or can recursive behaviour like this be generically simulated using regular SQL? A good example

Django self-recursive foreignkey filter query for all childs

喜你入骨 提交于 2019-11-27 18:49:51
I have this model with a self referencing Foreign Key relation: class Person(TimeStampedModel): name = models.CharField(max_length=32) parent = models.ForeignKey('self', null=True, blank=True, related_name='children') Now I want to get all the multi level children for a person. How do I write a Django query for it? It needs to behave like recursive function. You can always add a recursive function to your model: EDIT: Corrected according to SeomGi Han def get_all_children(self, include_self=True): r = [] if include_self: r.append(self) for c in Person.objects.filter(parent=self): _r = c.get

@ Symbol - a solution for Recursive SELECT query in Mysql?

徘徊边缘 提交于 2019-11-27 14:33:20
there are a lot of questions about Recursive SELECT query in Mysql, but most of answers is that "There NO solution for Recursive SELECT query in Mysql". Actually there is a certain solution & I want to know it clearly, so this question is the following of the previous question that can be found at ( how-to-do-the-recursive-select-query-in-mysql ) Suppose you have this table: col1 - col2 - col3 1 - a - 5 5 - d - 3 3 - k - 7 6 - o - 2 2 - 0 - 8 & you want to find all the links that connect to value "1" in col1, i.e. you want to print out: 1 - a - 5 5 - d - 3 3 - k - 7 Then you can use this

Recursive query in Oracle

删除回忆录丶 提交于 2019-11-27 07:15:10
问题 I am kind of new to the more advanced topics of PLSQL, so hopefully someone can help me out. The problem: I have a table with messages sent between an admin and users. The table has a message_parent with FK to the same table message_id field: in case the field is populated, then it means that message was sent as a reply to a previous message. I need to select all the messages that are part of the same conversation and display them. Can this be done with a single query or do I need a procedure

recursive cte with ranking functions

混江龙づ霸主 提交于 2019-11-27 06:29:06
问题 How to use ranking functions in recursive cte? Here's simple example showing how I'm trying to do: with cte as ( select 1 a, 1 b union all select 1, 2 union all select 2, 3 union all select 2, 4 ) , rcte (a, b, c, d) as ( select a, b, cast(0 as int), 1 from cte union all select a, b, cast(ROW_NUMBER() over (partition by a order by b) as int), d+1 from rcte where d < 2 ) select * from rcte where d=2 order by a, b Why there's no ranking? Show me my mistake pls 回答1: EDIT When you read the CTE

Prevent and/or detect cycles in postgres

天涯浪子 提交于 2019-11-27 06:11:15
问题 Assuming a schema like the following: CREATE TABLE node ( id SERIAL PRIMARY KEY, name VARCHAR, parentid INT REFERENCES node(id) ); Further, let's assume the following data is present: INSERT INTO node (name,parentid) VALUES ('A',NULL), ('B',1), ('C',1); Is there a way to prevent cycles from being created? Example: UPDATE node SET parentid = 2 WHERE id = 1; This would create a cycle of 1->2->1->... 回答1: Your trigger simplified and optimized, should be considerably faster: CREATE OR REPLACE

Django self-recursive foreignkey filter query for all childs

℡╲_俬逩灬. 提交于 2019-11-27 04:19:13
问题 I have this model with a self referencing Foreign Key relation: class Person(TimeStampedModel): name = models.CharField(max_length=32) parent = models.ForeignKey('self', null=True, blank=True, related_name='children') Now I want to get all the multi level children for a person. How do I write a Django query for it? It needs to behave like recursive function. 回答1: You can always add a recursive function to your model: EDIT: Corrected according to SeomGi Han def get_all_children(self, include

What is the equivalent PostgreSQL syntax to Oracle's CONNECT BY … START WITH?

爷,独闯天下 提交于 2019-11-27 02:39:13
问题 In Oracle , if I have a table defined as … CREATE TABLE taxonomy ( key NUMBER(11) NOT NULL CONSTRAINT taxPkey PRIMARY KEY, value VARCHAR2(255), taxHier NUMBER(11) ); ALTER TABLE taxonomy ADD CONSTRAINT taxTaxFkey FOREIGN KEY (taxHier) REFERENCES tax(key); With these values … key value taxHier 0 zero null 1 one 0 2 two 0 3 three 0 4 four 1 5 five 2 6 six 2 This query syntax … SELECT value FROM taxonomy CONNECT BY PRIOR key = taxHier START WITH key = 0; Will yield … zero one four two five six

Recursive stored functions in MySQL

你说的曾经没有我的故事 提交于 2019-11-27 02:23:08
问题 I'm trying to make a function that recursively builds a path for a specific category CREATE FUNCTION getPath(inId INT) RETURNS TEXT DETERMINISTIC BEGIN DECLARE return_path TEXT; DECLARE return_parent_id INT; SELECT CONCAT('/', name) INTO return_path FROM article_categories WHERE id = inId; SELECT parent_id INTO return_parent_id FROM article_categories WHERE id = inId; IF return_parent_id > 0 THEN SELECT CONCAT(getPath(return_parent_id), return_path) INTO return_path; END IF; RETURN return

Tree Structure and Recursion

女生的网名这么多〃 提交于 2019-11-27 01:38:53
问题 Using a PostgreSQL 8.4.14 database, I have a table representing a tree structure like the following example: CREATE TABLE unit ( id bigint NOT NULL PRIMARY KEY, name varchar(64) NOT NULL, parent_id bigint, FOREIGN KEY (parent_id) REFERENCES unit (id) ); INSERT INTO unit VALUES (1, 'parent', NULL), (2, 'child', 1) , (3, 'grandchild A', 2), (4, 'grandchild B', 2); id | name | parent_id ----+--------------+----------- 1 | parent | 2 | child | 1 3 | grandchild A | 2 4 | grandchild B | 2 I want to