如何在符合特定条件的情况下通过现场工作进行订购?

时间:2016-07-06 07:45:34

标签: mysql sql sql-order-by

我想通过字段topped_time来命令mysql查询结果,只有当它比现在早。例如,如果topped_time是2016-7-6,则应该考虑它,但如果topped_time是2016-7 -16,应该被忽略。

我试过

SELECT * FROM `article` ORDER BY IF(`topped_time` < CURRENT_TIME(), '`topped_time` DESC', ''), `published_time` DESC

SELECT * FROM `article` ORDER BY CASE WHEN `topped_time=` < CURRENT_TIME() THEN `topped_time` END, `published_time` DESC

仍然按topped_time排序,即使它比现在晚了。

这是表格:

CREATE TABLE `article` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `published_time` datetime DEFAULT '0000-00-00 00:00:00',
  `topped_time` datetime DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `article` (`id`, `published_time`, `topped_time`) VALUES
(1, '2016-07-05 22:01:14', '0000-00-00 00:00:00'),
(2, '2016-07-05 22:01:23', '0000-00-00 00:00:00'),
(3, '2016-07-05 22:01:25', '2016-07-07 00:00:00'),
(4, '2016-07-05 22:01:27', '0000-00-00 00:00:00'),
(5, '2016-07-05 22:01:29', '0000-00-00 00:00:00');

enter image description here

此屏幕截图中的正确顺序应为id:4,5,3,2,1,因为id 4的topped_time是2016-07-06 00:00:00,比现在早,应该是第一个。虽然id​​ 3的topped_time是2016-07-07 00:00:00,并且比现在晚,但应该被忽略。

正确的查询是什么或不可能?

5 个答案:

答案 0 :(得分:1)

按自定义列排序,该列应确定订单优先级:

SELECT *, 
    IF(`topped_time` < CURRENT_TIME(), 1, 0) AS topOrder 
FROM `article` 
ORDER BY topOrder DESC, `published_time` DESC

答案 1 :(得分:1)

试试这个:

SELECT * FROM `article`
ORDER BY CASE WHEN `topped_time=` < CURRENT_TIME()
              THEN 1
              ELSE 0
         END DESC,
         `published_time`  DESC

MySQL允许将布尔表达式计算为1和0.所以你可以尝试:

SELECT * FROM `article`
ORDER BY `topped_time=` > CURRENT_TIME(),
         `published_time`

答案 2 :(得分:1)

试试这个...

 SELECT * FROM `article`
  ORDER BY case
   WHEN `topped_time` <= now() THEN 
    `topped_time` 
   ELSE 
    0
   END,`published_time`  
  DESC

答案 3 :(得分:1)

我想这是你的选择:

SELECT * FROM `article`
ORDER BY 
    IF (`topped_time` < NOW(), `topped_time`, '0000-00-00 00:00:00') DESC, 
    `published_time`  DESC

+----+---------------------+---------------------+
| id | published_time      | topped_time         |
+----+---------------------+---------------------+
|  4 | 2016-07-05 22:01:27 | 2016-07-06 00:00:00 |
|  6 | 2016-07-05 22:01:28 | 2016-05-06 00:00:00 |
|  5 | 2016-07-05 22:01:29 | 0000-00-00 00:00:00 |
|  3 | 2016-07-05 22:01:25 | 2016-07-17 00:00:00 |
|  2 | 2016-07-05 22:01:23 | 0000-00-00 00:00:00 |
|  1 | 2016-07-05 22:01:14 | 0000-00-00 00:00:00 |
+----+---------------------+---------------------+

<强>更新

您可能会注意到我添加了额外的行来证明您的解决方案是错误的

SELECT * FROM article 
ORDER BY topped_time < CURRENT_TIME() AND 
         topped_time DESC, published_time DESC;

+----+---------------------+---------------------+
| id | published_time      | topped_time         |
+----+---------------------+---------------------+
|  6 | 2016-07-05 22:01:28 | 2016-05-06 00:00:00 |
|  4 | 2016-07-05 22:01:27 | 2016-07-06 00:00:00 |
|  5 | 2016-07-05 22:01:29 | 0000-00-00 00:00:00 |
|  3 | 2016-07-05 22:01:25 | 2016-07-17 00:00:00 |
|  2 | 2016-07-05 22:01:23 | 0000-00-00 00:00:00 |
|  1 | 2016-07-05 22:01:14 | 0000-00-00 00:00:00 |
+----+---------------------+---------------------+

答案 4 :(得分:1)

我认为我有一个有效的解决方案,但是还有很多不足之处。

SELECT 1 AS sort, a.* FROM (SELECT * FROM article
WHERE (topped_time < current_time()) AND (topped_time != '0000-00-00 00:00:00')
ORDER BY published_time DESC, topped_time DESC) AS a
UNION
SELECT 2 AS sort, b.* FROM (SELECT * FROM article
WHERE (topped_time > current_time()) OR (topped_time = '0000-00-00 00:00:00')
ORDER BY published_time DESC) as b
ORDER BY sort ASC, published_time DESC

enter image description here