简体   繁体   English

具有最大日期实例的SQL查询

[英]SQL query with max date instance

Given the following table: 给出下表:

DATE      |ARRANGMENT_STATUS | CUSTOMER
----------+------------------+-------
2017-01-01|BROKEN            |0001
2017-02-01|OK                |0001
2017-03-01|BROKEN            |0001

How can I query the DB so result will give me the latest broken status( 2017-03-01) - I dont want to get broken status if they are not the latest ( 2017-01-01) 我如何查询数据库,所以结果将给我最新的损坏状态(2017-03-01)-如果它们不是最新的,我不想得到损坏的状态(2017-01-01)

Hope it makes sence, thanks 希望有道理,谢谢

If you want all information in the row, you can use order by and limit : 如果要在行中显示所有信息,则可以使用order bylimit

select t.*
from t
where t.arrangement_status = 'Broken'
order by t.date desc
limit 1;

If you only want the date, the JNevill's solution is fine. 如果您只想要日期,JNevill的解决方案就可以了。

获取该状态的max(date):

SELECT max(date) FROM table WHERE arrangement_status = 'Broken'

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM