我有这张桌子:
self.delegate
我想按字母顺序排列`listId'的+----+--------+------+----------+
| id | listId | item | position |
+----+--------+------+----------+
| 4 | 2 | z234 | 1 |
| 5 | 2 | f324 | 2 |
| 2 | 2 | gt22 | 3 |
| 3 | 2 | aa11 | 4 |
| 1 | 2 | b321 | 5 |
+----+--------+------+----------+
列。所以最后,它应该是这样的:
item
如果我需要在同一张桌子上运行+----+--------+------+----------+
| id | listId | item | position |
+----+--------+------+----------+
| 3 | 2 | aa11 | 1 |
| 1 | 2 | b321 | 2 |
| 5 | 2 | f324 | 3 |
| 2 | 2 | gt22 | 4 |
| 4 | 2 | z234 | 5 |
+----+--------+------+----------+
按字母顺序排列项目,我该怎样UPDATE
?
谢谢
PS:我这样做的原因是因为这些项目在一个表格中,可以向上或向下拖动到一个新位置,但我希望用户能够按字母顺序排序并重新执行如果他们愿意,请订购
答案 0 :(得分:3)
这是一个解决方案:
SET @newPosition = 0;
SELECT id, listId, item, position, (@newPosition:=@newPosition + 1) AS newPosition
FROM foo
ORDER BY item;
输出:
| id | listId | item | position | newPosition |
|----|--------|------|----------|-------------|
| 3 | 2 | aa11 | 4 | 1 |
| 1 | 2 | b321 | 5 | 2 |
| 5 | 2 | f324 | 2 | 3 |
| 2 | 2 | gt22 | 3 | 4 |
| 4 | 2 | Z234 | 1 | 5 |
小提琴:http://www.sqlfiddle.com/#!9/24f51/2/1
编辑:要进行更新,您可以执行以下操作:
SET @newPosition = 0;
UPDATE foo SET position = (
SELECT tmp.newPosition FROM (
SELECT id,listId,item,position,(@newPosition:=@newPosition + 1) AS newPosition
FROM foo
ORDER BY item
) AS tmp WHERE tmp.id = foo.id
);
(sqlfiddle在这里有点破碎)完全小提琴:
MySQL 5.6架构设置:
-- +----+--------+------+----------+
-- | id | listId | item | position |
-- +----+--------+------+----------+
-- | 4 | 2 | z234 | 1 |
-- | 5 | 2 | f324 | 2 |
-- | 2 | 2 | gt22 | 3 |
-- | 3 | 2 | aa11 | 4 |
-- | 1 | 2 | b321 | 5 |
-- +----+--------+------+----------+
CREATE TABLE foo (
id INT,
listId INT,
item VARCHAR(5),
position INT
);
INSERT INTO foo VALUES
(4, 2, 'Z234', 1),
(5, 2, 'f324', 2),
(2, 2, 'gt22', 3),
(3, 2, 'aa11', 4),
(1, 2, 'b321', 5);
SET @newPosition = 0;
UPDATE foo SET position = (
SELECT tmp.newPosition FROM (
SELECT id,listId,item,position,(@newPosition:=@newPosition + 1) AS newPosition
FROM foo
ORDER BY item
) AS tmp WHERE tmp.id = foo.id
);
查询1 :
SELECT * FROM foo
<强> Results 强>:
| id | listId | item | position |
|----|--------|------|----------|
| 4 | 2 | Z234 | 5 |
| 5 | 2 | f324 | 3 |
| 2 | 2 | gt22 | 4 |
| 3 | 2 | aa11 | 1 |
| 1 | 2 | b321 | 2 |
答案 1 :(得分:2)
RC。刚刚用更新查询打败了我。 主要的区别是我在一个查询中执行它而不是两个查询。 我将用户变量设置在也可以工作的交叉连接中。
创建表/插入数据。
CREATE TABLE foo
(`id` INT, `listId` INT, `item` VARCHAR(4), `position` INT)
;
INSERT INTO foo
(`id`, `listId`, `item`, `position`)
VALUES
(4, 2, 'z234', 1),
(5, 2, 'f324', 2),
(2, 2, 'gt22', 3),
(3, 2, 'aa11', 4),
(1, 2, 'b321', 5)
;
<强>查询强>
用您自己的表名替换foo ..
UPDATE
foo
INNER JOIN (
SELECT
id
, (@newPosition:=@newPosition + 1) AS newPosition
FROM
foo
ORDER BY
item ASC
)
AS foo_table
CROSS JOIN (
SELECT @newPosition := 0
) AS init_user_var
SET
foo.position = foo_table.newPosition
WHERE
foo.id = foo_table.id
<强>结果强>
1 queries executed, 1 success, 0 errors, 0 warnings
Query: UPDATE foo INNER JOIN ( SELECT id , (@newPosition:=@newPosition + 1) AS newPosition FROM foo ORDER BY item ASC ) as foo_table CR...
5 row(s) affected
<强>查询强>
SELECT * FROM foo ORDER BY position ASC
<强>结果强>
id listId item position
------ ------ ------ ----------
3 2 aa11 1
1 2 b321 2
5 2 f324 3
2 2 gt22 4
4 2 z234 5