22,162
社区成员




select * from tab order by period desc limit 1
[/quote]
用排序的话只会对1-9进行排序,如果我有1-11期,排第一个就是9了[/quote]
----我用的是sql server 2017
if not object_id(N'Tempdb..#tab') is null
drop table #tab
Go
Create table #tab([name] nvarchar(21),[qname] nvarchar(24),[heji] int)
Insert #tab
select N'A',N'永久',10 union all
select N'A',N'12個月',20 union all
select N'A',N'6個月',30 union all
select N'A',N'3個月',40 union all
select N'A',N'永久',15 union all
select N'A',N'12個月',6 union all
select N'A',N'6個月',5 union all
select N'B',N'永久',20 union all
select N'B',N'12個月',30 union all
select N'B',N'6個月',40 union all
select N'B',N'3個月',50 union all
select N'B',N'永久',1
Go
select * from #tab
Select IDENTITY(INT,1,1) AS 序号,* into #aa from #tab
select * from #aa
select * from tab order by period desc limit 1
[/quote]
用排序的话只会对1-9进行排序,如果我有1-11期,排第一个就是9了select * from tab order by period desc limit 1
select top 1 * from tab order by period desc limit 1