如何找到一种类型的所有最新记录?

时间:2018-08-13 08:15:47

标签: mysql sql mariadb

我有一张这样的桌子:

id type value date
1  A    10    2018-08-01
2  A    20    2018-08-02
3  A    30    2018-08-04
4  B    11    2018-08-01
5  B    12    2018-08-02
6  C    11    2018-08-01

我想获得一种类型的最新记录:

id type value date
3  A    30    2018-08-04
5  B    12    2018-08-02
6  C    11    2018-08-01

如何编写此sql?

2 个答案:

答案 0 :(得分:1)

如果您与date有联系,则可以将limit子句与subquery一起使用:

select t.*
from table t
where id = (select t1.id
            from table t1
            where t1.type = t.type
            order by t1.date desc
            limit 1
           );

答案 1 :(得分:-1)

使用from django.contrib import admin from django.urls import path, include # For google login from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path('', include('home.urls')), # path('', include('pages.urls')), path('products/', include('products.urls')), path('reports/', include('reports.urls')), path('account/', include('social_django.urls', namespace='social')), path('account/', include('django.contrib.auth.urls', namespace='auth')), path('admin/', admin.site.urls), ] 函数和子查询

max()