how to work with recursive query in MySql?
-
WITH RECURSIVE transitive_closure(a, b, distance, path_string) AS ( SELECT a, b, 1 AS distance, a || '.' || b || '.' AS path_string, b AS direct_connection FROM edges2 WHERE a = 1 -- set the starting node UNION ALL SELECT tc.a, e.b, tc.distance + 1, tc.path_string || e.b || '.' AS path_string, tc.direct_connection FROM edges2 AS e JOIN transitive_closure AS tc ON e.a = tc.b WHERE tc.path_string NOT LIKE '%' || e.b || '.%' AND tc.distance < 3 ) SELECT * FROM transitive_closure --WHERE b=3 -- set the target node ORDER BY a,b,distance how to run this query in MySql? it will show error message like this: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'RECURSIVE transitive_closure(a, b, distance, path_string) AS ( SELECT a, b, 1 A' at line 1
-
Answer:
The WITH RECURSIVE statement/method is applicable in PostgreSQL and Sybase (and maybe a few more, I think), so maybe you can look at this instead: http://www.artfulsoftware.com/mysqlbook/sampler/mysqled1ch20.html It should show you some approaches using MySQL (and one or two in PHP, just to mention -- I know it's not in your tag list)
Maulik patel at Stack Overflow Visit the source
Related Q & A:
- How To Build Business Directory Using Php Mysql?Best solution by Stack Overflow
- How to convert my SQL query to MS Access query?Best solution by Stack Overflow
- How to save base64 to blob of MySQL?Best solution by stackoverflow.com
- How to declare variable in trigger with MySQL?Best solution by Stack Overflow
- How to Migrate from SQLite DB to MySQL?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.