Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Tabelle sortieren, Schleife in MySQL mit SELECT und UPDATE verwenden

SELECT @p:=0, @parent:=0;
UPDATE page p1
JOIN
( SELECT title, 
  CASE WHEN @parent<>parent_id THEN @p:=0 ELSE @p:[email protected]+1 END as position,
  @parent:=parent_id as parent_id
  FROM page
  ORDER BY parent_id, title DESC ) p2
ON p1.title = p2.title AND p1.parent_id = p2.parent_id
SET p1.position = p2.position

Wenn Ihre (parent_id,title)-Paare nicht eindeutig sind, verwenden Sie Ihren Primärschlüssel als Join-Bedingung - Sie müssen ihn hinzufügen, um ihn in Klammern auszuwählen.