简体   繁体   中英

Get nested set ancestor where condition

Given a nested set in mysql like this

+-------------+----------------------+-----+-----+------+
| category_id | name                 | lft | rgt | rank |
+-------------+----------------------+-----+-----+------+
|           1 | ELECTRONICS          |   1 |  20 |  99
|           2 | TELEVISIONS          |   2 |   9 |  50
|           3 | TUBE                 |   3 |   4 |  25
|           4 | LCD                  |   5 |   6 |  25
|           5 | PLASMA               |   7 |   8 |  25
|           6 | PORTABLE ELECTRONICS |  10 |  19 |  50
|           7 | MP3 PLAYERS          |  11 |  14 |  25
|           8 | FLASH                |  12 |  13 |  10
|           9 | CD PLAYERS           |  15 |  16 |  25
|          10 | 2 WAY RADIOS         |  17 |  18 |  20

How can I get an ancestor for eg: "Flash" where condition rank is >=50 AND <= 99 LIMIT 1 sorted by lft DESC. so the result will be

+-------------+----------------------+-----+-----+------+
| category_id | name                 | lft | rgt | rank |
+-------------+----------------------+-----+-----+------+
|           6 | PORTABLE ELECTRONICS |  10 |  19 |  50

for visualization refer to this link http://mikehillyer.com/media//categories.png

sql fiddle is not working for me right now, but you should be able to do something like this.

SELECT  
    c2.* 
FROM
    Categories as c1
    INNER JOIN Categories as c2 ON 
            c1.`lft` >= c2.lft AND c1.`lft` <= c2.`rgt` 
            AND c2.`RANK` >=50 AND c2.`RANK` <= 99 
WHERE 
    c1.`category_id` = 8
ORDER BY 
    c2.`lft` DESC
LIMIT 1;

SQL Fiddle

You dont have enough identifiers to differentiate between ancestors, having just "50" for a ranking doesnt differentiate the records.

If you did a

SELECT * FROM [SET] WHERE RANK >= 50 AND RANK <= 99

Would give you "Portable Electronics", "Televisions", and "Electronics", which are all technically ancestors.

If you added another column field, ie parent_id then it would make it much easier to build structure from the data.

+-------------+----------------------+-----+-----+------+---------+
| category_id | name                 | lft | rgt | rank |Parent_id|
+-------------+----------------------+-----+-----+------+---------+
|           1 | ELECTRONICS          |   1 |  20 |  99  | -
|           2 | TELEVISIONS          |   2 |   9 |  50  | 1
|           3 | TUBE                 |   3 |   4 |  25  | 2
|           4 | LCD                  |   5 |   6 |  25  | 2
|           5 | PLASMA               |   7 |   8 |  25  | 2
|           6 | PORTABLE ELECTRONICS |  10 |  19 |  50  | 1
|           7 | MP3 PLAYERS          |  11 |  14 |  25  | 6
|           8 | FLASH                |  12 |  13 |  10  | 6
|           9 | CD PLAYERS           |  15 |  16 |  25  | 6
|          10 | 2 WAY RADIOS         |  17 |  18 |  20  | 6

Or implementing a relationship table to hold those data sets

+------+--------+
| Item | Parent |
+------+--------+
|  2   |  1     |
|  3   |  2     |

I would have added as a comment, but i don't have enough reputation. hope this answers your question somewhat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM