开发者

How to do an operation only with consecutive rows by specified field in MySQL?

开发者 https://www.devze.com 2023-03-13 02:40 出处:网络
For example I have this table: TableX: codenameconsecutive 0a0 2b1 3开发者_如何学JAVAc2 1d3 5e0 4f1 6g2 7h0

For example I have this table:

TableX:

code       name     consecutive
0          a        0
2          b        1
3  开发者_如何学JAVA        c        2
1          d        3
5          e        0
4          f        1
6          g        2
7          h        0
9          i        1
10         j        2
8          k        3

I want to do this:

code          name              consecutive
(0,2,3,1)     (a,b,c,d,e)       (0,1,2,3)
(5,4,6)       (e,f,g)           (0,1,2)
(7,9,10)      (h,i,j)           (0,1,2)
(8)           (k)               (3)


SQL has no concept of "consecutive" in that sense. You'd have to define the grouping in your data model somewhere. There's nothing to say that your table wouldn't actually be stored in a different order like:

code       name     consecutive
0          a        0
2          b        1
6          g        2
8          k        3
5          e        0
4          f        1
3          c        2
1          d        3
7          h        0
9          i        1
10         j        2

If you want to specify ordering in a table, it can't rely on the "proximity" of other records because the way SQL databases store records is not in a consecutive structure like that at all behind the scenes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号