读书人

大家帮小弟我一下小弟我想用一条SQL

发布时间: 2011-12-30 23:30:45 作者: rapoo

大家帮我一下,我想用一条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


[解决办法]
个人觉得,如果没有主键,则必须用到临时表,因为要为其增加一个自增列来决定显示顺序呀.

读书人网 >SQL Server

热点推荐