很久以前我曾要求HERE帮助排序表格。用户" piotrm"感谢他给了一个优秀的解决方案!它完美无缺!
SELECT @p:=0, @parent:=0;
UPDATE page p1
JOIN
( SELECT title,
CASE WHEN @parent<>parent_id THEN @p:=0 ELSE @p:=@p+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
现在我需要使用NATURAL SORTING升级此代码。我的意思是它仍然需要按照相同的方式进行分类,但是当它遇到“TITLE STARTING WITH&#39; 2&#39;”,&#39; 13&#39;,&#39; 1&#39;,&#39; 26&#39;,必须订购NOT&#39; 1&#39;,&#39; 13&#39;,&#39; 2&#39;,&#39; 26&#39 ;;但是&#39; 1&#39;,&#39; 2&#39;,&#39; 13&#39;,&#39; 26&#39;。
我为SQL缺乏知识而道歉,但我真的需要你的帮助!非常感谢你!
答案 0 :(得分:0)
SELECT @p:=0, @parent:=0;
UPDATE page p1
JOIN
( SELECT title,
CASE WHEN @parent<>parent_id THEN @p:=0 ELSE @p:=@p+1 END as position,
@parent:=parent_id as parent_id
FROM page
ORDER BY LENGTH(title), title, parent_id ASC ) p2
ON p1.title = p2.title AND p1.parent_id = p2.parent_id
SET p1.position = p2.position