Solved

MySQL recursive query procedure

Posted on 2009-05-08
4
655 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 41

Expert Comment

by:pcelba
Comment Utility
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 41

Expert Comment

by:pcelba
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now