Solved

MySQL recursive query procedure

Posted on 2009-05-08
4
658 Views
Last Modified: 2012-05-06
I have a COMPARISONS table that has the following schema:
COMPARISONS
patch  (int)
replacement (int)
flag (int)  1(true) 0 (false)

For example:
patch   replacement   flag
1   2   0
2   3   0
3   7   1
4   7   1
5   7   1
6   8   1
7   8   1


I would like to know my descendant patches (comparison flag must be == 1).  
So if I enter 8,  it will display 3,4,5,6,7.  The depth of the tree is not limited.

Would need your help in creating a procedure or SQL statement for this in MySQL


   
0
Comment
Question by:angelsky
  • 2
4 Comments
 
LVL 42

Expert Comment

by:pcelba
ID: 24335411
Somehow I don't see recursivity from your example because you can easily write

SELECT patch
  FROM COMPARISONS
 WHERE replacement <= 8 AND flag = 1
 ORDER BY patch

0
 
LVL 42

Expert Comment

by:pcelba
ID: 24335511
If you need all patches on one line then GROUP_CONCAT() could work (I am not sure because I don't have MySQL by hand):  

SELECT GROUP_CONCAT(patch) As Patches
  FROM COMPARISONS
 WHERE replacement <= 8 AND flag = 1
0
 
LVL 19

Accepted Solution

by:
NerdsOfTech earned 200 total points
ID: 24469389
http://www.artfulsoftware.com/mysqlbook/sampler/mysqled1ch20.html

Here is an example of a similar procedure yours can conform to.
DROP PROCEDURE IF EXISTS ListReached;
DELIMITER |
 
CREATE PROCEDURE ListReached( IN root CHAR(1) )
BEGIN
  DECLARE rows SMALLINT DEFAULT 0;
  DROP TABLE IF EXISTS reached;
  CREATE TABLE reached (
    nodeID CHAR(1) PRIMARY KEY
  ) ENGINE=HEAP;
  INSERT INTO reached VALUES (root );
  SET rows = ROW_COUNT();
  WHILE rows > 0 DO
    INSERT IGNORE INTO reached
      SELECT DISTINCT childID
      FROM edges AS e
      INNER JOIN reached AS p ON e.parentID = p.nodeID;
    SET rows = ROW_COUNT();
    INSERT IGNORE INTO reached
      SELECT DISTINCT parentID
      FROM edges AS e
      INNER JOIN reached AS p ON e.childID = p.nodeID;
    SET rows = rows + ROW_COUNT();
  END WHILE;
  SELECT * FROM reached;
  DROP TABLE reached;
END;
|
DELIMITER ;
CALL ListReached('A');

Open in new window

0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question