这是我的查询
$query="SELECT parent.name, parent.depth
FROM address AS node,
address AS parent
WHERE node.name LIKE '%".$name."%' AND
node.lft BETWEEN parent.lft AND parent.rgt
order by node.lft, parent.rgt";
这是我的表结构:
name |lft | rgt | depth|
------------+----+-----+------+
Australia | 1 | 10 | 1 |
nsw | 2 | 9 | 2 |
sydney | 3 | 8 | 3 |
kensington | 4 | 5 | 4 |
kingsford | 6 | 7 | 4 |
------------+----+-----+---- -+
让我们假设$name
= k然后结果将是
name |depth|
------------+-----+
kingsford | 4 |
sydney | 3 |
nsw | 2 |
Australia | 1 |
kensington | 4 |
sydney | 3 |
nsw | 2 |
Australia | 1 |
我会像这样介绍他们:
-->kensington, sydney, nsw, australia
-->kingsford, sydney, nsw, australia
我无法将此限制为最多只能覆盖10个地址。如果我添加了limit 2
,例如它将不会显示上面的两个地址。它只会显示-->kensington, sydney
。我怎么能这样做?
答案 0 :(得分:0)
也许问题不太清楚,但如果我理解正确,我会使用这样的东西:
SELECT
GROUP_CONCAT(parent.name ORDER BY parent.depth DESC) AS name
FROM
address AS node JOIN address AS parent
ON node.lft BETWEEN parent.lft AND parent.rgt
WHERE node.name LIKE '%k%'
GROUP BY node.name
LIMIT 10
请参阅小提琴here。
修改强>
您可能还想使用它,以原始格式返回行:
SELECT
parent.name, parent.depth
FROM
(SELECT name, lft FROM address WHERE name LIKE '%k%' LIMIT 10) AS node,
address AS parent
WHERE
node.lft BETWEEN parent.lft AND parent.rgt
order by node.lft, parent.rgt