Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.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中对数据进行分块编号?_Mysql - Fatal编程技术网

如何在MySQL中对数据进行分块编号?

如何在MySQL中对数据进行分块编号?,mysql,Mysql,我想连续地对文件集的块进行编号,例如从1到3 假设我有以下几点: ID | random_nr | ordering .. | .. | .. 19 | 12 | 2 19 | 21 | 3 20 | 13 | 1* 20 | 14 | 2* 20 | 22 | 3* 21 | 15 | 1 21 | 17 | 2 .. | .. | 到目前为止,我的代码只包含所有集合的数字: SET @c = 0; SELECT `ID`, `random`, (@c := @c + 1) AS "or

我想连续地对文件集的块进行编号,例如从1到3

假设我有以下几点:

ID | random_nr | ordering
.. | .. | ..
19 | 12 | 2
19 | 21 | 3
20 | 13 | 1*
20 | 14 | 2*
20 | 22 | 3*
21 | 15 | 1
21 | 17 | 2
.. | .. | 
到目前为止,我的代码只包含所有集合的数字:

SET @c = 0; 
SELECT
`ID`,
`random`,
(@c := @c + 1) AS "ordering"
FROM `tabelle` WHERE ...
我怎样才能改变它,从而得到上面提到的结果? 万分感谢

这里有一个方法

 SELECT x.*
      , COUNT(*) ordering 
   FROM tabelle x 
   JOIN tabelle y 
     ON y.id = x.id 
    AND y.random_nr <= x.random_nr 
  GROUP 
     BY x.id
      , x.random_nr;
选择x*
,计数(*)排序
来自泰贝尔x
加入tabelle y
在y.id=x.id上
和y.random_nr

因此,每次ID不等于@i的当前值时,@c从1开始。

问题不清楚
SET @i = null;
SELECT
`ID`,
`random`,
(@c := IF(ID = @i, @c + 1, 1) + (@i:=ID)-ID AS "ordering"
FROM `tabelle` WHERE ...
ORDER BY ID