日期(和字符串)反向排序

问题描述 投票:0回答:1

我的 MySQL 服务器似乎对日期(和字符串)进行排序,与我的预期相反。 较早的日期不应该小于较晚的日期吗? 我得到了相反的结果。 这是怎么回事? 这里有一些根本性的错误,要么是我的理解,要么是我的服务器。

数字很好。

mysql> SELECT CAST('2023-11-23' AS date) BETWEEN CAST('2004-01-01' AS date) AND CAST('2024-12-31' AS date);
+----------------------------------------------------------------------------------------------+
| CAST('2023-11-23' AS date) BETWEEN CAST('2004-01-01' AS date) AND CAST('2024-12-31' AS date) |
+----------------------------------------------------------------------------------------------+
|                                                                                            1 |
+----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select CAST('2023-11-23' AS date) > CAST('2004-01-01' AS date);
+---------------------------------------------------------+
| CAST('2023-11-23' AS date) > CAST('2004-01-01' AS date) |
+---------------------------------------------------------+
|                                                       1 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select CAST('2023-11-23' AS date) < CAST('2004-01-01' AS date);
+---------------------------------------------------------+
| CAST('2023-11-23' AS date) < CAST('2004-01-01' AS date) |
+---------------------------------------------------------+
|                                                       0 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT '2023-11-23' BETWEEN '2004-01-01' AND '2024-12-31';
+----------------------------------------------------+
| '2023-11-23' BETWEEN '2004-01-01' AND '2024-12-31' |
+----------------------------------------------------+
|                                                  1 |
+----------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT '2023-11-23' > '2004-01-01';
+-----------------------------+
| '2023-11-23' > '2004-01-01' |
+-----------------------------+
|                           1 |
+-----------------------------+
1 row in set (0.00 sec)

mysql> SELECT '2023-11-23' < '2004-01-01';
+-----------------------------+
| '2023-11-23' < '2004-01-01' |
+-----------------------------+
|                           0 |
+-----------------------------+
1 row in set (0.00 sec)

mysql> SELECT 1 BETWEEN 4 AND 10;
+--------------------+
| 1 BETWEEN 4 AND 10 |
+--------------------+
|                  0 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT 7 BETWEEN 4 AND 10;
+--------------------+
| 7 BETWEEN 4 AND 10 |
+--------------------+
|                  1 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT 13 BETWEEN 4 AND 10;
+---------------------+
| 13 BETWEEN 4 AND 10 |
+---------------------+
|                   0 |
+---------------------+
1 row in set (0.00 sec)

mysql> select VERSION();
+-------------------------+
| VERSION()               |
+-------------------------+
| 8.0.40-0ubuntu0.22.04.1 |
+-------------------------+
1 row in set (0.00 sec)

mysql>

我最初尝试发布我的问题失败了,因为它说该帖子似乎主要是代码,所以我可以添加更多细节。 所以我只是意识流地输入,直到该消息消失。 我对这些瑕疵表示最诚挚的歉意。 我不想这么做,但这个网站让我这么做了。 我实在没有什么其他有用的话可说。 但它就在那里。

哦,来吧! 还需要多少。

这越来越无聊了。

我相信你也同意。

mysql date
1个回答
0
投票

嘎! 编辑历史记录的麻烦。 当我在第一个单词上表示“2024”时,我输入了“2004”,并且同样的错误继续存在。 对此感到抱歉。 没问题。 (当然希望我可以删除我自己的问题。)

© www.soinside.com 2019 - 2024. All rights reserved.