2

I'm trying to understand how MySQL recursive queries work and to get the data the way I want it.

Right now I have the following SQL

SELECT `Library`.*, l2.*, l3.*
FROM `Library`
LEFT JOIN `Library` as `l2` on `l2`.`subsectionOf`=`Library`.`iD`
LEFT JOIN `Library` as `l3` on `l3`.`subsectionOf`=`l2`.`iD`
WHERE `Library`.`iD` = 0x02
ORDER BY `Library`.`subsectionOrder`, `l2`.`subsectionOrder`, `l3`.`subsectionOrder`

This gets me a 3-level-deep hierarchy of data laid out as:

Layer 1 fields, Layer 2 fields, Layer 3 fields 
  1. How to I make the query work for n levels of depth?
  2. How do I make the results stack on top of each other rather than adding fields to the right?

    Layer 1 Row
    Layer 2 Row
    Layer 2 Row
    Layer 3 Row
    Layer 3 Row
    Layer 2 Row
    Layer 3 Row
    Layer 2 Row
    
Andriy M
  • 76,112
  • 17
  • 94
  • 154
Justin808
  • 20,859
  • 46
  • 160
  • 265
  • It's not a recursive query (there is no such thing). It's a simple self join (table joined to itself). The limit of joins in one query currently is hardcoded at 63, so keep that in mind. – Mchl Nov 17 '11 at 17:46
  • 1
    @Mchl: Oracle, Microsoft SQL Server, DB2, and PostgreSQL support recursive queries. See http://www.postgresql.org/docs/current/static/queries-with.html – Bill Karwin Nov 17 '11 at 17:51
  • Anyway, no such thing in MySQL ;) Thanks for enlightement ;) – Mchl Nov 17 '11 at 18:55

3 Answers3

3

MySQL does not support recursive SQL queries. Most people store hierarchical data differently so they can get whole trees in a single query.

See my answer to What is the most efficient/elegant way to parse a flat table into a tree?

I have a presentation Models for Hierarchical Data with SQL and PHP, and I wrote about tree-structured data in my book SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming.

Another tricky solution is given by @Quassnoi in his answer to Hierarchical queries in MySQL here on SO.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • I can highly recommend Bill's presentations. Many interesting tips and solutions not only for hierarchical data. – Mchl Nov 17 '11 at 18:59
1

Your query is not a recursive query; it's just a table self-joining itself 3 times.

A 5 minutes Google search will show you that, as of now, MySQL does not support truly recursive queries.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
  • I did a 5 minute google search for `MySQL SQL recursive query` and got a bunch of results that looked like the one I posted above. Sorry for misunderstanding what I was reading. – Justin808 Nov 17 '11 at 17:56
1

MySQL does not support recursive querying.

In other databases, you can perform recursion by using Common Table Expressions (CTEs), for example with SQL Server, Postgres, and DB2. With Oracle, you can use CTEs (as of 11g), or the non-standard CONNECT BY. MySQL does not support either of these options.

bhamby
  • 15,112
  • 1
  • 45
  • 66
  • As of Oracle 11, there's no need to use the proprietary CONNECT BY syntax. Oracle now supports recursive queries with the same syntax used by Microsoft and DB2. PostgreSQL also supports similar recursive CTE syntax. – Bill Karwin Nov 17 '11 at 17:54