[英]MySql MAX(DATE(Transaction_Date)) returns different type than DATE(MAX(Transaction_Date))
I have a MySql 5.7 transaction table with a DATETIME
column, Transaction_Date
, that is indexed.我有一个带有DATETIME
列Transaction_Date
的 MySql 5.7 事务表,该表已被索引。 My Python 3.8.5 program is interested in retrieving the maximum transaction date on the table (ignoring the time portion).我的 Python 3.8.5 程序对检索表上的最大交易日期感兴趣(忽略时间部分)。 There are two possible queries (maybe even more):有两种可能的查询(甚至更多):
select date(max(Transaction_Date)) from `transaction`
and和
select max(date(Transaction_Date)) from `transaction`
However, depending on which query I use, pymysql
or mysql.connector
(it doesn't matter which one I use) returns me a different data type as the result, ie a date.datetime
instance for the first query and a str
for the second:但是,根据我使用的查询, pymysql
或mysql.connector
(我使用哪个并不重要)返回不同的数据类型作为结果,即第一个查询的date.datetime
实例和第一个查询的str
第二:
The Table:桌子:
CREATE TABLE `transaction` (
`Transaction_ID` varchar(32) NOT NULL,
`Transaction_Date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `transaction`
ADD PRIMARY KEY (`Transaction_ID`),
ADD KEY `Transaction_Date` (`Transaction_Date`);
The Program:该程序:
import pymysql
database = 'xxxx'
user_id = 'xxxx'
password = 'xxxx'
conn = pymysql.connect(db=database, user=user_id, passwd=password, charset='utf8mb4', use_unicode=True)
cursor = conn.cursor()
cursor.execute('select date(max(Transaction_Date)) from `transaction`')
row = cursor.fetchone()
d = row[0]
print(d, type(d))
cursor.execute('select max(date(Transaction_Date)) from `transaction`')
row = cursor.fetchone()
d = row[0]
print(d, type(d))
Prints:印刷:
2021-01-19 <class 'datetime.date'>
2021-01-19 <class 'str'>
Can anyone explain why a datetime.date
is not returned for the second query?谁能解释为什么第二个查询没有返回datetime.date
? For what it's worth, I have another table with a DATE
column and when I select the MAX
of that column I am returned a datetime.date
instance.对于它的价值,我有另一个带有DATE
列的表,当我 select 该列的MAX
时,我返回了一个datetime.date
实例。 So why am I not returned a datetime.date
for a MAX(DATE(column_name))
?那么为什么我没有为MAX(DATE(column_name))
返回datetime.date
呢?
Update更新
mysql> create temporary table t1 as select max(date(Transaction_Date)) as d from `transaction`;
Query OK, 1 row affected (0.20 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> show columns from t1;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| d | date | YES | | NULL | |
+-------+------+------+-----+---------+-------+
1 row in set (0.01 sec)
This is what I would expect, so it is quite a puzzle.这是我所期望的,所以这是一个难题。
I looked through pymysql
source and some MySQL documentation.我查看了pymysql
源代码和一些 MySQL 文档。 It looks like the database server returns field descriptors, which the module then uses to set a class on the value, there are various converters available: https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/converters.py .看起来数据库服务器返回字段描述符,然后模块使用它在值上设置 class,有各种可用的转换器: https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/converters.py . At the bottom, it maps field types to the method.在底部,它将字段类型映射到方法。
The database server is describing max(date)
as a VAR_STRING, so the module proceeds accordingly.数据库服务器将max(date)
描述为 VAR_STRING,因此模块会相应地进行。 I'm not sure of the specific reason for this description, which I suppose is the core of your question.我不确定这个描述的具体原因,我想这是你问题的核心。 It would take some digging through MySQL source, the documentation is not very detailed.通过 MySQL 源代码需要一些挖掘,文档不是很详细。
As a workaround, casting the result to a date does make it work as expected:作为一种解决方法,将结果转换为日期确实可以使其按预期工作:
select cast(max(date(Transaction_Date)) as date) from transaction
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.