gpt4 book ai didi

Mysql查询为每次迭代按顺序对记录进行排序?

转载 作者:行者123 更新时间:2023-11-29 17:15:27 25 4
gpt4 key购买 nike

我的表结构如下.. NumberSeqCode 我想在中创建 Seq每个 Number 的序列 1.2.3..4 的顺序都是相同的,此序列会随着 Number 中的每次更改而变化..下一个 Seq从 1.2 开始...到 max..就像所需的输出一样..如何做到这一点?任何帮助将不胜感激..提前致谢..

CREATE TABLE `unsorted` (
`id` int(11) NOT NULL auto_increment,
`Number` int(5) default NULL,
`Seq` int(3) default NULL,
`Code` varchar(6) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=31 ;

--
-- Dumping data for table `unsorted`
--

INSERT INTO `unsorted` (`id`, `Number`, `Seq`, `Code`) VALUES
(1, 107, 3, 'KRMI'),
(2, 107, 4, 'MAO'),
(3, 107, 1, 'SWV'),
(4, 107, 2, 'THVM'),
(5, 108, 2, 'KRMI'),
(6, 108, 1, 'MAO'),
(7, 108, 4, 'SWV'),
(8, 108, 3, 'THVM'),
(9, 401, 1, 'AWB'),
(10, 401, 9, 'AY'),
(11, 401, 8, 'BBK'),
(12, 401, 12, 'BSB'),
(13, 401, 3, 'BSL'),
(14, 401, 6, 'CNB'),
(15, 401, 5, 'JHS'),
(16, 401, 11, 'JNU'),
(17, 401, 4, 'KNW'),
(18, 401, 7, 'LKO'),
(19, 401, 2, 'MMR'),
(20, 401, 10, 'SHG'),
(21, 421, 4, 'BE'),
(22, 421, 2, 'FGR'),
(23, 421, 3, 'KSJ'),
(24, 421, 1, 'LKO'),
(25, 421, 5, 'SVDK'),
(26, 422, 2, 'BE'),
(27, 422, 4, 'FGR'),
(28, 422, 3, 'KSJ'),
(29, 422, 5, 'LKO'),
(30, 422, 1, 'SVDK');

期望的输出:

CREATE TABLE `sorted` (
`id` int(11) NOT NULL auto_increment,
`Number` int(5) default NULL,
`Seq` int(3) default NULL,
`Code` varchar(6) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=31 ;

--
-- Dumping data for table `sorted`
--

INSERT INTO `sorted` (`id`, `Number`, `Seq`, `Code`) VALUES
(1, 107, 1, 'SWV'),
(2, 107, 2, 'THVM'),
(3, 107, 3, 'KRMI'),
(4, 107, 4, 'MAO'),
(5, 108, 1, 'MAO'),
(6, 108, 2, 'KRMI'),
(7, 108, 3, 'THVM'),
(8, 108, 4, 'SWV'),
(9, 401, 1, 'AWB'),
(10, 401, 2, 'MMR'),
(11, 401, 3, 'BSL'),
(12, 401, 4, 'KNW'),
(13, 401, 5, 'JHS'),
(14, 401, 6, 'CNB'),
(15, 401, 7, 'LKO'),
(16, 401, 8, 'BBK'),
(17, 401, 9, 'AY'),
(18, 401, 10, 'SHG'),
(19, 401, 11, 'JNU'),
(20, 401, 12, 'BSB'),
(21, 421, 1, 'LKO'),
(22, 421, 2, 'FGR'),
(23, 421, 3, 'KSJ'),
(24, 421, 4, 'BE'),
(25, 421, 5, 'SVDK'),
(26, 422, 1, 'SVDK'),
(27, 422, 2, 'BE'),
(28, 422, 3, 'KSJ'),
(29, 422, 4, 'FGR'),
(30, 422, 5, 'LKO');

最佳答案

您可以尝试类似的查询:

SELECT * From table Order by Number, Seq Asc

关于Mysql查询为每次迭代按顺序对记录进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51664767/

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