Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 当WHERE子句中的id来自上一行时选择数据_Mysql_Sql - Fatal编程技术网

Mysql 当WHERE子句中的id来自上一行时选择数据

Mysql 当WHERE子句中的id来自上一行时选择数据,mysql,sql,Mysql,Sql,我有两张桌子。第一个保存最后一个操作的id,第二个保存操作的id和一些数据 mysql> select * from t1; +----+--------------+ | id | id_last_oper | +----+--------------+ | 1 | 45 | +----+--------------+ mysql> select * from t2; +-------+------------+---------+ | fo_id | fo

我有两张桌子。第一个保存最后一个操作的id,第二个保存操作的id和一些数据

mysql> select * from t1;
+----+--------------+
| id | id_last_oper |
+----+--------------+
|  1 |           45 |
+----+--------------+

mysql> select * from t2;
+-------+------------+---------+
| fo_id | fo_prev_id | fo_name |
+-------+------------+---------+
|     1 |       NULL | a       |
|     5 |          1 | a       |
|    22 |          5 | a       |
|    45 |         22 | a       |
+-------+------------+---------+
我的问题是:

select fo_id from t2 
           where fo_id=44 and @id:=fo_prev_id 
    union select fo_id from t2 where fo_id=@id
    union ...;

我需要选择所有行,但行数是非永久性的,我无法使用simple
UNION
选择所有行。我怎样才能实现我的目标?

几个月前我也遇到了同样的问题。 有两种解决办法

解决方案1(推荐)-处理“左/右关系”
关系数据库不是为层次结构而设计的。这个链接帮助我改变了表格的结构,使用了层次结构。它快速且易于使用:
我建议您使用此解决方案,因为它独立于您使用的数据库,而且我的经验始终是积极的

解决方案2-存储过程
在这种情况下,可以递归调用存储过程本身。在这句话中,这是很好的解释。如果向下滚动更多,请在解决方案1中查看相同的链接。

我不确定这是否是您想要的:

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>

结果应该是?所有行都是chain@juergend他想要一个递归的分层查询。您需要在
fou prev\u id为null之前提取记录吗?
?是。只有第一个操作具有空值