更新列以使其包含行位置

Sal*_*n A 11 php mysql sql

这是content表:

ContentID | CategoryID | Position | Other1 | Other2
===================================================
1         | 1          | NULL     | abcd   | efgh
2         | 1          | NULL     | abcd   | efgh
3         | 1          | NULL     | abcd   | efgh
4         | 2          | NULL     | abcd   | efgh
5         | 2          | NULL     | abcd   | efgh
6         | 2          | NULL     | abcd   | efgh
Run Code Online (Sandbox Code Playgroud)

这些是我将要运行的查询:

SELECT ContentID FROM content WHERE CategoryID = 1 ORDER BY Position
SELECT ContentID FROM content WHERE CategoryID = 2 ORDER BY Position
Run Code Online (Sandbox Code Playgroud)

现在我想实现向上移动,向下移动,移动到顶部并移动到底部函数以获取内容.我需要做的就是用数字填充Position列:

ContentID | CategoryID | Position
=================================
1         | 1          | 1
2         | 1          | 2
3         | 1          | 3
4         | 2          | 1
5         | 2          | 2
6         | 2          | 3
Run Code Online (Sandbox Code Playgroud)

是否有可能通过MySQL中的单个查询来实现这一点?就像是:

UPDATE content
SET Position = <ROW_NUMBER>
WHERE CategoryID = 1
ORDER BY Position

UPDATE content
SET Position = <ROW_NUMBER>
WHERE CategoryID = 2
ORDER BY Position
Run Code Online (Sandbox Code Playgroud)

ajr*_*eal 11

这应该工作

update 
content,
(
  select 
  @row_number:=ifnull(@row_number, 0)+1 as new_position,
  ContentID 
  from content
  where CategoryID=1
  order by position
) as table_position
set position=table_position.new_position
where table_position.ContentID=content.ContentID;
Run Code Online (Sandbox Code Playgroud)

但我更愿意先应用它,以取消设置用户定义的变量

set @row_number:=0;
Run Code Online (Sandbox Code Playgroud)

由Mchl添加:

你可以在这样的一个声明中做到这一点

update 
content,
(
  select 
  @row_number:=ifnull(@row_number, 0)+1 as new_position,
  ContentID 
  from content
  where CategoryID=1
  order by position
) as table_position,
(
  select @row_number:=0
) as rowNumberInit
set position=table_position.new_position
where table_position.ContentID=content.ContentID;
Run Code Online (Sandbox Code Playgroud)