我在此表中有一个嵌套列表address
:
name |lft | rgt |
------------+----+-----+
Australia | 1 | 10 |
nsw | 2 | 9 |
sydney | 3 | 8 |
kensington | 4 | 5 |
kingsford | 6 | 7 |
------------+----+-----+
表格说明:如您所见,Australia 包括从左右值到其下方的所有其他值。这同样适用于新南威尔士州和悉尼,它们包含它们下方的内容。kensington 和 kingsford 的深度一样,都属于悉尼。
我想在这里写一个自动完成搜索的查询。我想当我输入“k”时得到以下结果:
-->kensington, sydney, nsw, australia
-->kingsford, sydney, nsw, australia
当前查询:
'SELECT parent.name
FROM address AS node,
address AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name LIKE ' . $input
我的查询确实返回相同的值,但“混合”如下:
Array
(
[0] => australia
[name_en] => australia
)
Array
(
[0] => australia
[name_en] => australia
)
Array
(
[0] => NSW
[name_en] => NSW
)
Array
(
[0] => NSW
[name_en] => NSW
)
Array
(
[0] => sydney
[name_en] => sydney
)
Array
(
[0] => sydney
[name_en] => sydney
)
Array
(
[0] => kensington
[name_en] => kensington
)
Array
(
[0] => kingsford
[name_en] => kingsford
)
我寻求的结果是这样的
Array
(
[0] => australia
[name_en] => australia
)
Array
(
[0] => NSW
[name_en] => NSW
)
Array
(
[0] => sydney
[name_en] => sydney
)
Array
(
[0] => kensington
[name_en] => kensington
)
Array
(
[0] => australia
[name_en] => australia
)
Array
(
[0] => NSW
[name_en] => NSW
)
Array
(
[0] => sydney
[name_en] => sydney
)
Array
(
[0] => kingsford
[name_en] => kingsford
)
它应该一个接一个地返回路径。
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/