USE test
DROP TABLE IF EXISTS ordered_names;
CREATE TABLE ordered_names
(
id int not null auto_increment,
firstname VARCHAR(20),
lastname VARCHAR(20),
PRIMARY KEY (id),
KEY name_ndx (lastname,firstname)
);
INSERT INTO ordered_names (firstname,lastname) VALUES
('pamela','edwards'),('rolando','edwards'),('diamond','edwards');
ALTER TABLE ordered_names ADD COLUMN sort_order INT DEFAULT NULL;
SELECT * FROM ordered_names;
INSERT INTO ordered_names (firstname,lastname) VALUES
('javonne','washington'),('richard','washington'),
('carlik','washington'),('dominique','edwards');
SET @x = 0;
UPDATE ordered_names SET sort_order = (@x:=@x+1) ORDER BY lastname,firstname;
SELECT * FROM ordered_names;
让我们从一个表开始,将数据放入其中,添加一个排序顺序列
让我们实际运行它:
接下来,让我们填充排序顺序列:
让我们运行它:
好的,到目前为止很好,让我们再添加四 (4) 个人并重新排序:
让我们运行它:
试试看 !!!