表名税。
id | name | value | created_at | updated_at
:----: | :----: | :----: | :-------: | :----:
1 | VAT | 9.56 | 2017-01-10 09:25:40 | 2017-01-10 09:25:40
:----- | :----- | :----: | :----- | :----
2 | Service Tax | 10.56 | 2017-01-10 10:25:40 | 2017-01-10 10:25:40
:----- | :----- | :----: | :----- | :----
3 | Service charge | 11.56 | 2017-01-10 11:25:40 | 2017-01-10 11:25:40
:----- | :----- | :----: | :----- | :----
4 | vat | 2.56 | 2017-01-12 08:25:40 | 2017-01-12 08:25:40
:----- | :----- | :----: | :----- | :----
5 | service charge | 21.56 | 2017-01-13 09:25:40 | 2017-01-13 09:25:40
:----- | :----- | :----: | :----- | :----
6 | Service tax | 21.56 | 2017-01-21 09:25:40 | 2017-01-21 09:25:40
:----- | :----- | :----: | :----- | :----
7 | Vat | 12.56 | 2017-01-23 09:25:40 | 2017-01-23 09:25:40
:----- | :----- | :----: | :----- | :----
8 | Serivce Charge | 16.56 | 2017-01-23 10:25:40 | 2017-01-23 10:25:40
:----- | :----- | :----: | :----- | :----
我必须检索每个税的最新创建的税。
预期输出:
id | name | value | created_at | updated_at
:----: | :----: | :----: | :-------: | :----:
6 | Service tax| 21.56 | 2017-01-21 09:25:40 | 2017-01-21 09:25:40
:----- | :----- | :----: | :----- | :----
7 | Vat | 12.56 | 2017-01-23 09:25:40 | 2017-01-23 09:25:40
:----- | :----- | :----: | :----- | :----
8 | Serivce Charge | 16.56 | 2017-01-23 10:25:40 | 2017-01-23 10:25:40
请帮帮我,提前谢谢你。
最佳答案
SQL Server和Oracle
with taxes as
(
select id, name, value, row_number() over(partition by name order by created_at desc) as tax_ord
from table1
)
select *
from taxes
where tax_ord = 1
MySQL数据库
select a2.*
from
(select name, max(created_at) as Max_Create
from table1
group by name) a1
inner join table1 a2
on a1.name = a2.name
and a1.Max_Create = a2.created_at
关于mysql - SQL查询,用于检索具有相同名称并最近创建的记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41566094/