recursive MySQL query

1

Good afternoon colleagues, I hope you can help me with this query that drives me crazy. You will see I have a table with data like these

And I need an SQL query I order the data in this way

  • Vertebrates
  • Mono
  • Cat
  • Dog
  • Horse
  • Mula
  • Invertebrates
  • Snake
  • Grasshopper
  • Fly
  • Little starfish

How could you formulate the query? Thank you very much

    
asked by Ariel Chepito 18.04.2017 в 04:09
source

2 answers

1

This query gets what you're looking for:

 SELECT distinct t2.descripcion AS DESCRIPCION 
  FROM Animalitos as t1 
LEFT JOIN Animalitos AS t2 ON (t2.id_referencia = t1.id OR t1.id = t2.id ) 
ORDER BY t1.id_referencia ASC, t1.id ASC, t2.id ASC, t2.id_referencia ASC

I hope you serve, greetings.

    
answered by 18.04.2017 / 05:20
source
1

You can sort it like this, add a new column ' es_vertebrado ' ( for example ) of type int, where 1 , indicates that it is vertebrate, and 0 that is invertebrate. The query would look like this:

SELECT * FROM animalito ORDER BY es_vertebrado DESC, ID ASC
    
answered by 18.04.2017 в 04:53