gpt4 book ai didi

mysql - 如何在满足特定条件的情况下才通过现场工作下订单?

转载 作者:行者123 更新时间:2023-11-29 01:27:05 25 4
gpt4 key购买 nike

我想按字段topped_time对mysql的查询结果进行排序,只在topped_time早于now的时候。比如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,比现在晚,应该忽略。

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

最佳答案

我想这是你的选择:

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 |
+----+---------------------+---------------------+

关于mysql - 如何在满足特定条件的情况下才通过现场工作下订单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38218738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com