大家帮我一下,我想用一条SQL语句来获取最后一条记录的话SQL语句怎么写?
谢谢大家帮忙了!
------解决方案--------------------如何从表中用SELECT语句提取最后10条数据呢?
declare @num as int
select @num = count(*) from authors
set @num = @num - 10
declare @sql as varchar(200)
set @sql = 'select * from authors where au_id not in (select top ' + cast(@num as char) + ' au_id from authors) '
exec (@sql)
------解决方案--------------------如何从表中用SELECT语句提取最后1条数据呢?
declare @num as int
select @num = count(*) from authors
set @num = @num - 1
declare @sql as varchar(200)
set @sql = 'select * from authors where au_id not in (select top ' + cast(@num as char) + ' au_id from authors) '
exec (@sql)
------解决方案--------------------select identity(int,1,1) as id,* into # from area
select * from # a where not exists (select 1 from # where id> a.id)
drop table #
------解决方案--------------------select identity(int,1,1) as id,* into # from 表
select * from # a where not exists (select 1 from # where id> a.id)
drop table #
------解决方案--------------------如果从速度上
用临时表比较不错的方法
------解决方案--------------------select identity(int,1,1) as id,* into # from 表
select * from # a where not exists (select 1 from # where id> a.id)
drop table #
-------------------
就是这样的.
------解决方案--------------------下面的方法也很好,你试一下:
select identity(int,1,1) as id,* into # from table1
select * from # a where id in (select max(id) from #)
drop table #
------解决方案--------------------select identity(int,1,1) as id,* into # from 表
select top 1 * from # order by id desc
------解决方案--------------------一.如果有主键,则:
select top 1 * from table order by 主键 desc
二.如没有主键,则:
select identity(int,1,1) as rowid,* into # from table
select top 1 * from # order by rowid desc
------解决方案--------------------个人觉得,如果没有主键,则必须用到临时表,因为要为其增加一个自增列来决定显示顺序呀.