获取嵌套集合祖先 where 条件
Get nested set ancestor where condition
给定 mysql 中的嵌套集,如下所示
+-------------+----------------------+-----+-----+------+
| 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
我怎样才能获得祖先,例如:"Flash",其中条件排名 >=50 AND <= 99 LIMIT 1 按 lft DESC 排序。
所以结果将是
+-------------+----------------------+-----+-----+------+
| category_id | name | lft | rgt | rank |
+-------------+----------------------+-----+-----+------+
| 6 | PORTABLE ELECTRONICS | 10 | 19 | 50
可视化参考这个link
http://mikehillyer.com/media//categories.png
您没有足够的标识符来区分祖先,只有“50”的排名并不能区分记录。
如果你做了
SELECT * FROM [SET] WHERE RANK >= 50 AND RANK <= 99
会给你 "Portable Electronics"、"Televisions" 和 "Electronics",它们在技术上都是祖先。
如果您添加了另一个列字段,即 parent_id,则可以更轻松地从数据构建结构。
+-------------+----------------------+-----+-----+------+---------+
| 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
或者实现一个关系table来保存那些数据集
+------+--------+
| Item | Parent |
+------+--------+
| 2 | 1 |
| 3 | 2 |
我会添加为评论,但我没有足够的声誉。希望这能在一定程度上回答您的问题。
sql fiddle 现在不适合我,但你应该可以做这样的事情。
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;
给定 mysql 中的嵌套集,如下所示
+-------------+----------------------+-----+-----+------+
| 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
我怎样才能获得祖先,例如:"Flash",其中条件排名 >=50 AND <= 99 LIMIT 1 按 lft DESC 排序。 所以结果将是
+-------------+----------------------+-----+-----+------+
| category_id | name | lft | rgt | rank |
+-------------+----------------------+-----+-----+------+
| 6 | PORTABLE ELECTRONICS | 10 | 19 | 50
可视化参考这个link http://mikehillyer.com/media//categories.png
您没有足够的标识符来区分祖先,只有“50”的排名并不能区分记录。
如果你做了
SELECT * FROM [SET] WHERE RANK >= 50 AND RANK <= 99
会给你 "Portable Electronics"、"Televisions" 和 "Electronics",它们在技术上都是祖先。
如果您添加了另一个列字段,即 parent_id,则可以更轻松地从数据构建结构。
+-------------+----------------------+-----+-----+------+---------+
| 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
或者实现一个关系table来保存那些数据集
+------+--------+
| Item | Parent |
+------+--------+
| 2 | 1 |
| 3 | 2 |
我会添加为评论,但我没有足够的声誉。希望这能在一定程度上回答您的问题。
sql fiddle 现在不适合我,但你应该可以做这样的事情。
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;