我正在尝试按升序或降序对日期表进行排序,但我都无法执行它。该查询未给出语法错误,因此我认为不是。
Server version: 5.7.28-0ubuntu0.18.04.4 (Ubuntu)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
mysql> select * from elections order by 'last_election' ASC;
+--------+-----------------+---------------+
| row_id | jurisdiction_id | last_election |
+--------+-----------------+---------------+
| 1 | 3 | 2018-11-24 |
| 2 | 2 | 2019-03-23 |
| 4 | 5 | 2018-03-17 |
| 5 | 4 | 2017-11-25 |
| 6 | 1 | 2016-10-15 |
| 7 | 6 | 2017-03-11 |
| 8 | 7 | 2018-03-03 |
| 9 | 8 | 2016-08-27 |
| 11 | 9 | 2019-05-18 |
+--------+-----------------+---------------+
9 rows in set (0.00 sec)
上一个/其他类似的问题都说,“您的字段类型是文本还是varchar ,,!”但是字段类型绝对是日期:
+----------------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+---------+------+-----+---------+----------------+
| row_id | int(11) | NO | PRI | NULL | auto_increment |
| jurisdiction_id | int(11) | NO | UNI | NULL | |
| last_election | date | YES | | NULL | |
+----------------------+---------+------+-----+---------+----------------+
我如何查询表以便last_election正确?
似乎您的日期列不是日期时间类型。排序时必须将其转换为日期时间:
select * from elections order by CONVERT(datetime, last_election) ASC;
尝试这个。希望它能解决您的问题