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

时间:2018-08-03 03:08:54

标签: mysql sql sorting

我具有如下表结构。NumberSeqCode我想使Seq的顺序像序列1.2.3..4 Number是相同的,Number中的每一次更改都会改变此顺序。下一个Seq从1.2开始...到最大。如期望的输出一样。任何帮助将不胜感激..谢谢您。

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');

1 个答案:

答案 0 :(得分:0)

您可以尝试查询:

SELECT * From table Order by Number, Seq Asc