Max*_*sov 7 mysql sql hibernate
我有桌子:
+----+--------+----------+
| id | doc_id | next_req |
+----+--------+----------+
| 1 | 1 | 4 |
| 2 | 1 | 3 |
| 3 | 1 | 0 |
| 4 | 1 | 2 |
+----+--------+----------+
Run Code Online (Sandbox Code Playgroud)
id - auto incerement主键.
nex_req - 表示记录的顺序.(next_req = 记录的id)
如何构建SQL查询以此顺序获取记录:
+----+--------+----------+
| id | doc_id | next_req |
+----+--------+----------+
| 1 | 1 | 4 |
| 4 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 1 | 0 |
+----+--------+----------+
Run Code Online (Sandbox Code Playgroud)
解释:
record1 with id=1 and next_req=4 means: next must be record4 with id=4 and next_req=2
record4 with id=5 and next_req=2 means: next must be record2 with id=2 and next_req=3
record2 with id=2 and next_req=3 means: next must be record3 with id=1 and next_req=0
record3 with id=3 and next_req=0: means that this is a last record
Run Code Online (Sandbox Code Playgroud)
我需要在表格中存储记录顺序.这对我来说很重要.
我能够为您提供 Oracle 中的解决方案,
select id,doc_id,next_req from table2
start with id =
(select id from table2 where rowid=(select min(rowid) from table2))
connect by prior next_req=id
Run Code Online (Sandbox Code Playgroud)