简体   繁体   English

如何获取 mysql 中具有最大日期值的那些行的记录?

[英]How do I fetch the records of those rows which have the maximum date value in mysql?

My sql query:我的 sql 查询:

SELECT * FROM `electric_transaction` 
WHERE `Current Date`=(SELECT MAX(STR_TO_DATE(`Current Date`, '%d/%m/%Y')) 
FROM `electric_transaction`)

When I execute the above query, I get a list of warnings showing:当我执行上述查询时,我会得到一个警告列表,其中显示:

Warning: #1292 Truncated incorrect datetime value: '01/04/2021'警告:#1292 截断不正确的日期时间值:'01/04/2021'

Warning: #1292 Truncated incorrect datetime value: '31/03/2021'...警告:#1292 截断不正确的日期时间值:'31/03/2021'...

I've even tried我什至试过

 SELECT * FROM `electric_transaction` 
 WHERE `Current Date`=(SELECT MAX(`Current Date`) FROM 
 `electric_transaction`)

But, it just shows all the records which don't even have the maximum date.但是,它只显示所有甚至没有最大日期的记录。 How do I fetch only those records which have the maximum date?如何仅获取具有最大日期的那些记录?

Note: My Current Date column is of type VARCHAR and the dates are in d/m/Y format注意:我的Current Date列是VARCHAR类型,日期是d/m/Y格式

As suggested by @Akina this query works:正如@Akina 所建议的,这个查询有效:

SELECT * FROM `electric_transaction` 
WHERE `Current Date`=
(SELECT DATE_FORMAT(MAX(STR_TO_DATE(`Current Date`, '%d/%m/%Y')),
 '%d/%m/%Y') FROM `electric_transaction`)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用mySQL选择具有一列最大值的结果,按另一列分组? - How do I use mySQL to select results which have a maximum value for one column, grouped by another column? 如何识别mysql中相同列中具有相同值的行? 而且我还必须向这些行添加条件 - How to identify rows that have the same value in same columns in mysql? and also i have to add condition to those rows 如何遍历mysql表中的行并删除在给定字段中具有特定值的行? - How to loop through rows in mysql table and delete those which have a certain value in a given field? 搜索带有&列值的记录 - search those records which have column value with & in it mysql我试图消除不包含最大日期的所有行 - mysql I seek to eliminate all rows that do not contain the maximum date 获取不属于列表的mySQL记录 - Fetch mySQL records which do not fall in list MySql获取具有最新日期的行 - MySql Fetch rows that have latest date CODEIGNITER:如何获取截止日期为明天的所有行? - CODEIGNITER: How do I fetch all of the rows that have a due date until tomorrow? 如何从MySQL表创建唯一记录列表,然后使用这些记录计算均值 - How do I create list of unique records from a MySQL table, and then use those records to calculate means 如何只读取那些没有锁定的记录? - How to read only those records which have no lock on them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM