gpt4 book ai didi

php - 使用 php 重新排序父/子行 mysql

转载 作者:行者123 更新时间:2023-11-29 02:04:49 26 4
gpt4 key购买 nike

我需要通过表格中的表单提交行对嵌套页面布局的以下结构进行重新排序

列:(表名是:sourcedocs3)

sort1 | sort2 | type

1 | 1 | parent

1 | 2 | child

2 | 1 | no nesting

3 | 1 | parent

3 | 2 | child

3 | 3 | child

4 | 1 | no nesting

我需要能够在不丢失基本结构的情况下(通过 PHP/Mysql)将 3 重新排序为 1 —— 3 变为 1,所有值为 1 的行都递增)。听起来很简单,但我很难在改组后保持结构完整(3|1 应该是 1|1,3|2 应该是 1|2 等等)

最佳答案

START TRANSACTION;

UPDATE `why_do_people_never_give_the_table_name` SET sort1 = 999
WHERE sort1 = 3;

UPDATE `why_do_people_never_give_the_table_name` SET sort1 = sort1 + 1
WHERE sort1 BETWEEN 1 AND 3
ORDER BY sort1 DESC;

UPDATE `why_do_people_never_give_the_table_name` SET sort1 = 1
WHERE sort1 = 999;

COMMIT;

请注意,如果您将菜单移动到后面的位置,例如将 2 移动到 4,您需要按升序排列第二次更新。

关于php - 使用 php 重新排序父/子行 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8406459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com