两个日期字段上的Mysql查询排序

时间:2015-02-25 04:14:38

标签: mysql

我正在尝试对两个日期字段(开始和结束)中的数据进行排序。但是有一个特殊的条件。它应该平行地对两个字段进行排序。

我的用户表

id | start                  | end
---------------------------------------------------
1  | 2015-02-24 00:00:00    | 2015-02-18 00:00:00
---------------------------------------------------
2  | 2015-02-24 00:00:00    | 2015-02-03 00:00:00
---------------------------------------------------
3  | 2015-02-25 00:00:00    | NULL
---------------------------------------------------
4  | 2015-02-23 00:00:00    | NULL
---------------------------------------------------

预期产出:

id | start                  | end
---------------------------------------------------
3  | 2015-02-25 00:00:00    | NULL
---------------------------------------------------
4  | 2015-02-23 00:00:00    | NULL
---------------------------------------------------
1  | 2015-02-24 00:00:00    | 2015-02-18 00:00:00
---------------------------------------------------
2  | 2015-02-24 00:00:00    | 2015-02-03 00:00:00
---------------------------------------------------

正如您所见,ID为1的行位于第3位,因为结束日期的值存在且小于上一个记录的开始日期。

表格应同时对两个字段进行排序。它应该给结束字段更高的优先级。但是如果结束字段为空,则应该在START字段上进行排序。

我尝试了以下查询:

SELECT * FROM `users` order by end DESC, start DESC 
SELECT * FROM `users` order by start DESC,least(start,end) DESC 

上面的查询给了我一个类似的结果但是你可以看到它没有考虑结束日期。我的意思是,但那不是我想要的:

id | start                  | end
---------------------------------------------------
3  | 2015-02-25 00:00:00    | NULL
---------------------------------------------------
1  | 2015-02-24 00:00:00    | 2015-02-18 00:00:00
---------------------------------------------------
2  | 2015-02-24 00:00:00    | 2015-02-03 00:00:00
---------------------------------------------------
4  | 2015-02-23 00:00:00    | NULL
---------------------------------------------------

如果要执行并尝试,下面是创建表和示例数据。

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) NOT NULL,
  `start` datetime DEFAULT NULL,
  `end` datetime DEFAULT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `start`, `end`) VALUES
(1, '2015-02-24 00:00:00', '2015-02-03 00:00:00'),
(2, '2015-02-24 00:00:00', '2015-02-18 00:00:00'),
(3, '2015-02-25 00:00:00', NULL),
(4, '2015-02-23 00:00:00', NULL);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `users`
--
ALTER TABLE `users`
 ADD PRIMARY KEY (`id`);

ALTER TABLE `users`
MODIFY `id` int(10) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=5;

2 个答案:

答案 0 :(得分:0)

好吧,我想我明白你要做什么 - 这个问题有点令人困惑。试试这个:

SELECT * FROM `users` order by IF(end IS NULL, start, end) DESC

有关详情,请参阅Can you add an if statement in ORDER BY?

答案 1 :(得分:0)

查询: 的 SQLFIDDLEExample

SELECT * 
FROM `users` 
order by COALESCE(end,start)  DESC, start DESC 

结果:

| ID |                           START |                 END |
|----|---------------------------------|---------------------|
|  3 | February, 25 2015 00:00:00+0000 |              (null) |
|  4 | February, 23 2015 00:00:00+0000 |              (null) |
|  1 | February, 24 2015 00:00:00+0000 | 2015-02-18 00:00:00 |
|  2 | February, 24 2015 00:00:00+0000 | 2015-02-03 00:00:00 |