Alternatively mysql recursive method using the function IFNULL

Is the relationship between categories and categories as well as categories of goods in question and electricity supplier industry often encountered.

If you want to query all the goods in one category

Conventional practice is to query all two categories this one category, then check all three categories of two categories, until the last one, then queries the corresponding goods.

The method first thought is recursive.

 

Mysql stumbled IFNULL method can actually replace conventional recursive method.

Let me use under IFNULL method:

What is the role of IFNULL? A simple sql following description and results, if IFNULL (a, b), a received value is null, B is returned, otherwise a;

For example: a query id in all categories of the bottom according to a category Level 1 Category

 

The left join is based on several layers you have to set the category. If only category 3 layer, the above c4 can be removed.

 

Such a sql can replace a recursive method.

 

Guess you like

Origin www.cnblogs.com/wang-yaz/p/11051646.html