Least intensive way to find all nested subcategories using mysql query

I have a database in which items are placed in categories. Some of these categories are nested, for example:

Animals > Birds > Parrots  
Animals >  Birds > Penguin 
Animals > Mammals > Cats  
Animals > Mammals > Dogs 
Animals > Reptiles > Snakes 
Plants > Trees 
Plants > Flowers

etc.

I have them in a table along the rows

CATEGORY    PARENT
Animals     -
Birds       Animals
Penguin     Birds

etc.

I would like to be able to take the starting point, say, “Animals”, and list all the subcategories that fall under this, so for animals we would indicate “Birds, mammals, reptiles, parrots, penguins, cats, Snakes

Is this possible with a single request? If not, what do I need to do

TIA

+3
source share
4 answers

, .

(start end). , start node end (.. ).

, , (start,end):

Animals (1,18)
   Birds (2,7)
      Parrots (3,6 )
      Penguin (4,5)
   Mammals (8,13)
      Cats (9,12)
      Dogs (10,11)
   Reptiles (14,17)
      Snakes (15,16)
Plants (19,24)
   Trees (20,23)
   Flowers (21,22)

, get Animals , - :

SELECT * FROM Category where start >=1 and start < 18

, , , . .

+1

, , SQL, " ":

google.com " + Sitepoint", sitepoint.com, .

+1

SQL, , .

.

( )

  • A
  • index = 0
  • < ()
    • (A [i])
    • index = index + 1

, , , ,

0
source

I don't know much about mysql procedures for retrieving data, but we can get nested data with a single mysql query using PHP, you can use the same logic for any language.

http://www.maheshchari.com/php-nested-categories-with-single-mysql-query/

0
source

Source: https://habr.com/ru/post/1747450/


All Articles