这是我的声明:
SELECT (
COUNT( parent.categoryName ) - ( sub_tree.depth +1 ) ) AS depth,
CONCAT( REPEAT( '', ( COUNT( parent.categoryName ) - ( sub_tree.depth +1 ) ) ) , node.categoryName ) AS categoryName
FROM
Categories AS node,
Categories AS parent,
Categories AS sub_parent,
(
SELECT node.categoryName, (
COUNT( parent.categoryName ) -1) AS depth
FROM
Categories AS node,
Categories AS parent
WHERE
node.categoryLft BETWEEN parent.categoryLft AND parent.categoryRgt
AND node.categoryName LIKE 'Product'
GROUP BY node.categoryName
ORDER BY node.categoryLft
) AS sub_tree
WHERE
node.categoryLft BETWEEN parent.categoryLft AND parent.categoryRgt
AND node.categoryLft BETWEEN sub_parent.categoryLft AND sub_parent.categoryRgt
AND sub_parent.categoryName = sub_tree.categoryName
GROUP BY node.categoryName
ORDER BY node.categoryLft
它工作得很好,但我想修改它以仅获取所选类别(此处为“产品”)旁边的第一个节点,而没有子类别的子类别
喜欢:产品:
A型
亚型A
亚型B
B型
我想得到'TypeA','TypeB'。
顺便说一下,这是我的桌子:
CREATE TABLE `Categories` (
`categoryId` int(11) NOT NULL auto_increment,
`categoryLft` int(11) NOT NULL,
`categoryRgt` int(11) NOT NULL,
`categoryName` varchar(255) default NULL,
`categoryAlias` varchar(255) default NULL,
PRIMARY KEY (`categoryId`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8