我在mySQL中有一个像这样的数据库表,并希望使用当前值child创建树结果。
id item_id secondary_id
1 1 1
2 1 1
3 1 1
4 1 3
5 1 3
6 1 3
7 1 3
8 1 3
9 1 3
10 1 3
11 1 3
12 1 1
13 1 1
14 1 1
15 1 1
16 1 7
17 1 7
18 1 7
19 1 7
20 1 7
我想得到这样的最终结果:
$current_value = 18;
id = 1
id = 2
id = 3
id = 6
id = 7
id = 16
id = 17
id = 18
< - 此目标$current_value
id = 19
id = 20
id = 8
id = 9
id = 12
id = 13
我使用此SQL查询,但只从目标获取结果当前目标,先前值和下一个值。
$query = "(
SELECT *
FROM (
SELECT *
FROM table_name
WHERE id < $current_target
ORDER BY id
LIMIT 2
) AS comment
ORDER BY comment.id ASC
)
UNION ALL
(
SELECT *
FROM (
SELECT *
FROM table_name
WHERE id = $current_target
ORDER BY id DESC
LIMIT 2
) AS comment
ORDER BY comment.id ASC
)
UNION ALL
(
SELECT *
FROM (
SELECT *
FROM table_name
WHERE id > $current_target
ORDER BY id
LIMIT 2
) AS comment
ORDER BY comment.id
)";
我希望将结果提升到Root。