如何在满足特定条件的情况下才通过现场工作下订单?
How to make order by field work only when it meets specific condition?
我想按字段topped_time对mysql查询结果进行排序,仅当它早于now.For例如,如果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 订购,即使比现在晚了。
这里是 table:
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');
这个截图的正确顺序应该是id:4,5,3,2,1,因为id 4的topped_time是2016-07-06 00:00:00,比现在早,应该是第一个。而id 3的topped_time是2016-07-0700:00:00,比现在晚,应该忽略。
正确的查询是什么还是不可能的?
按应确定顺序优先级的自定义列排序:
SELECT *,
IF(`topped_time` < CURRENT_TIME(), 1, 0) AS topOrder
FROM `article`
ORDER BY topOrder DESC, `published_time` DESC
试试这个:
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`
试试这个...
SELECT * FROM `article`
ORDER BY case
WHEN `topped_time` <= now() THEN
`topped_time`
ELSE
0
END,`published_time`
DESC
我想这是你的选择:
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 |
+----+---------------------+---------------------+
我想我有一个可行的解决方案,但还有很多不足之处。
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
我想按字段topped_time对mysql查询结果进行排序,仅当它早于now.For例如,如果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 订购,即使比现在晚了。
这里是 table:
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');
这个截图的正确顺序应该是id:4,5,3,2,1,因为id 4的topped_time是2016-07-06 00:00:00,比现在早,应该是第一个。而id 3的topped_time是2016-07-0700:00:00,比现在晚,应该忽略。
正确的查询是什么还是不可能的?
按应确定顺序优先级的自定义列排序:
SELECT *,
IF(`topped_time` < CURRENT_TIME(), 1, 0) AS topOrder
FROM `article`
ORDER BY topOrder DESC, `published_time` DESC
试试这个:
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`
试试这个...
SELECT * FROM `article`
ORDER BY case
WHEN `topped_time` <= now() THEN
`topped_time`
ELSE
0
END,`published_time`
DESC
我想这是你的选择:
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 |
+----+---------------------+---------------------+
我想我有一个可行的解决方案,但还有很多不足之处。
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