私は本当にわからない溶液中で同じリンクを参照してください。
SELECT result.*
FROM (
SELECT
(select fo_id FROM t2 where fo_id = @xid) as ids
, @xid := (select `fo_prev_id` FROM t2 where fo_id = @xid) as next_id
, @row := @row +1 as row_order
FROM t2 AS dummy
CROSS JOIN (SELECT @xid := 45, @row := 0) as init
) AS orderd_ids
LEFT JOIN t2 AS result ON result.`fo_id` = orderd_ids.ids
ORDER BY orderd_ids.row_order;
サンプル
mysql> SELECT * FROM t2; +-------+------------+---------+
| fo_id | fo_prev_id | fo_name |
+-------+------------+---------+
| 1 | NULL | a |
| 5 | 1 | a |
| 22 | 5 | a |
| 45 | 22 | a |
+-------+------------+---------+
4 rows in set (0,00 sec)
mysql> SELECT result.*
-> FROM (
-> SELECT
-> (select fo_id FROM t2 where fo_id = @xid) as ids
-> , @xid := (select `fo_prev_id` FROM t2 where fo_id = @xid) as next_id
-> , @row := @row +1 as row_order
-> FROM t2 AS dummy
-> CROSS JOIN (SELECT @xid := 45, @row := 0) as init
->) AS orderd_ids
-> LEFT JOIN t2 AS result ON result.`fo_id` = orderd_ids.ids
-> ORDER BY orderd_ids.row_order;
+-------+------------+---------+
| fo_id | fo_prev_id | fo_name |
+-------+------------+---------+
| 45 | 22 | a |
| 22 | 5 | a |
| 5 | 1 | a |
| 1 | NULL | a |
+-------+------------+---------+
4 rows in set (0,00 sec)
mysql>
サンプル2
mysql> SELECT * FROM t2;
+-------+------------+---------+
| fo_id | fo_prev_id | fo_name |
+-------+------------+---------+
| 1 | 66 | a |
| 5 | 1 | a |
| 22 | 5 | a |
| 45 | 22 | a |
| 66 | 72 | a |
| 72 | NULL | a |
+-------+------------+---------+
6 rows in set (0,00 sec)
mysql> SELECT result.*
-> FROM (
-> SELECT
-> (select fo_id FROM t2 where fo_id = @xid) as ids
-> , @xid := (select `fo_prev_id` FROM t2 where fo_id = @xid) as next_id
-> , @row := @row +1 as row_order
-> FROM t2 AS dummy
-> CROSS JOIN (SELECT @xid := 45, @row := 0) as init
->) AS orderd_ids
-> LEFT JOIN t2 AS result ON result.`fo_id` = orderd_ids.ids
-> ORDER BY orderd_ids.row_order;
+-------+------------+---------+
| fo_id | fo_prev_id | fo_name |
+-------+------------+---------+
| 45 | 22 | a |
| 22 | 5 | a |
| 5 | 1 | a |
| 1 | 66 | a |
| 66 | 72 | a |
| 72 | NULL | a |
+-------+------------+---------+
6 rows in set (0,00 sec)
mysql>
結果は? –
チェーンのようなすべての行 – vit
@juergend彼は再帰的階層クエリを望んでいます。 –