我有一个SQL Fiddle中所示的表格。
CREATE TABLE `test` (
`id` varchar(250) NOT NULL,
`name` varchar(512) NOT NULL,
`description` text NOT NULL,
`priority` int(11) DEFAULT NULL,
`created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `test` (`id`, `name`, `description`, `priority`, `created_date`) VALUES
('1', 'John', 'kasdjkaksdj', 3, '2018-07-12 07:37:25'),
('2', 'Doe', 'updations', 1, '2018-07-12 08:56:27'),
('3', 'Matt', 'sdasd', NULL, '2018-07-12 09:09:08'),
('4', 'Jiss', 'vghjgfghj', 1, '2018-07-13 12:33:19'),
('5', 'Noel', 'sdfsdf', NULL, '2018-07-13 12:33:29');
priority
列默认为NULL
。用户可以在此列中指定任何整数值。我想根据priority asc
对表进行排序,对于其余记录(具有NULL优先级),按created_date asc
进行排序。
我尝试了查询
SELECT * FROM `test` ORDER BY priority, created_date
结果:
id name description priority created_date 3 Matt sdasd (null) 2018-07-12T09:09:08Z 5 Noel sdfsdf (null) 2018-07-13T12:33:29Z 2 Doe updations 1 2018-07-12T08:56:27Z 4 Jiss vghjgfghj 1 2018-07-13T12:33:19Z 1 John kasdjkaksdj 3 2018-07-12T07:37:25Z
但这显示的是优先级,优先级为NULL
,然后是按优先级排序的记录。
例外结果:
id name description priority created_date 2 Doe updations 1 2018-07-12T08:56:27Z 4 Jiss vghjgfghj 1 2018-07-13T12:33:19Z 1 John kasdjkaksdj 3 2018-07-12T07:37:25Z 3 Matt sdasd (null) 2018-07-12T09:09:08Z 5 Noel sdfsdf (null) 2018-07-13T12:33:29Z
谁能帮助我找到正确的查询。预先感谢。