[英]MySQL query to maintain sort-order in ascending order for self-referencing table
I am using MySQL 5.6.17
. 我正在使用
MySQL 5.6.17
。
I have a self-referencing
table let us say TableA
with columns id (int), title (varchar(100)), parent_id(int), sort_order (int)
. 我有一个
self-referencing
表,让我们说TableA
带有列id (int), title (varchar(100)), parent_id(int), sort_order (int)
。
The parent_id
column is a foreign key
that refers to the id
of the same table. parent_id
列是一个foreign key
,它引用同一表的id
。 In this way I maintain the N level
of hierarchy. 这样,我保持了
N level
层次结构。
The table data is as below : 表数据如下:
id title parent sort_order
1 Item 1 NULL 1
2 Item 1.1 1 1
3 Item 1.2 1 4
4 Item 1.3 1 5
5 Item 2 NULL 3
6 Item 2.1 5 1
7 Item 2.1.1 6 4
8 Item 2.1.2 6 5
9 Item 2.2 5 3
10 Item 2.1.3 6 3
Here, the hierarchy is well maintained but NOT the sort-order . 在这里,层次结构维护得很好,但排序顺序却没有 。 I want to re-order the items under each parent item.
我想重新排序每个父项下的项。
The resultant data should be like below : 结果数据应如下所示:
id title parent sort_order
1 Item 1 NULL 1
2 Item 1.1 1 1
3 Item 1.2 1 2
4 Item 1.3 1 3
5 Item 2 NULL 2
6 Item 2.1 5 1
7 Item 2.1.1 6 1
8 Item 2.1.2 6 2
9 Item 2.2 5 2
10 Item 2.1.3 6 3
I have tried the query shown below to re-order
the sort order of each item under parent : 我试过下面显示的查询来
re-order
排序父项下每个项目的排序顺序:
UPDATE TableA
CROSS JOIN (SELECT @rownumber := 0) r
SET TableA.sort_order = (@rownumber := @rownumber + 1)
WHERE TableA.parent IN (SELECT t.id FROM TableA t);
But it returns error 但它返回错误
Error Code: 1093
You can't specify target table 'TableA' for update in FROM clause
If I remove the where
clause from the above query then it resets the sort order for each item in ascending order but I want each item under parent starts with sort order 1 . 如果我从上面的查询中删除了
where
子句,那么它将按升序重置每个项目的排序顺序,但是我希望父项下的每个项目都从排序顺序1开始 。
Any idea how to achieve it? 知道如何实现吗?
Thanks in advance. 提前致谢。
Sample data: 样本数据:
CREATE TABLE t
(`id` int, `title` varchar(10), `parent` varchar(4), `sort_order` int)
;
INSERT INTO t
(`id`, `title`, `parent`, `sort_order`)
VALUES
(1, 'Item 1', NULL, 1),
(2, 'Item 1.1', '1', 1),
(3, 'Item 1.2', '1', 4),
(4, 'Item 1.3', '1', 5),
(5, 'Item 2', NULL, 3),
(6, 'Item 2.1', '5', 1),
(7, 'Item 2.1.1', '6', 4),
(8, 'Item 2.1.2', '6', 5),
(9, 'Item 2.3', '5', 3),
(10, 'Item 2.1.3', '6', 3)
;
Query: 查询:
update t
join (
select
t.*,
@so := if(coalesce(parent, '0') != @p, 1, @so + 1) as new_sort_order
, @p := coalesce(parent, '0')
from t,
(select @so := 0, @p := null) var_init
order by parent, id
) sq on t.id = sq.id
set t.sort_order = sq.new_sort_order;
Result: 结果:
select * from t;
| ID | TITLE | PARENT | SORT_ORDER |
|----|------------|--------|------------|
| 1 | Item 1 | (null) | 1 |
| 2 | Item 1.1 | 1 | 1 |
| 3 | Item 1.2 | 1 | 2 |
| 4 | Item 1.3 | 1 | 3 |
| 5 | Item 2 | (null) | 2 |
| 6 | Item 2.1 | 5 | 1 |
| 7 | Item 2.1.1 | 6 | 1 |
| 8 | Item 2.1.2 | 6 | 2 |
| 9 | Item 2.3 | 5 | 2 |
| 10 | Item 2.1.3 | 6 | 3 |
Keep in mind, that you have to specify a column in the order by that determines the sort_order. 请记住,您必须在确定sort_order的顺序中指定一列。 I was assuming it's
id
in this case. 在这种情况下,我假设它是
id
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.