--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([ID] int,[品名] varchar(6),[入库数量] int,[入库时间] datetime)
insert [a]
select 1,'矿泉水',100,'2013-01-02' union all
select 2,'方便面',60,'2013-01-03' union all
select 3,'方便面',50,'2013-01-03' union all
select 4,'矿泉水',80,'2013-01-04' union all
select 5,'方便面',50,'2013-01-05'
select top 3 * from (select top 4 * from a order by id asc) b order by id desc
/*
ID 品名 入库数量 入库时间
----------- ------ ----------- -----------------------
4 矿泉水 80 2013-01-04 00:00:00.000
3 方便面 50 2013-01-03 00:00:00.000
2 方便面 60 2013-01-03 00:00:00.000
(3 行受影响)
*/
SQLServer基础语法实例应用(二)(三)
矿泉水',100,'2013-01-02' union all
select 2,'方便面',60,'2013-01-03' union all
select 3,'方便面',50,'2013-01-03' union all
select 4,'矿泉水',80,'2013-01-04' union all
select 5,'方便面',50,'2013-01-05'
select * from a
TRUNCATE TABLE a
select * from a
/*
(5 行受影响)
ID 品名 入库数量 入库时间
----------- ------ ----------- -----------------------
1 矿泉水 100 2013-01-02 00:00:00.000
2 方便面 60 2013-01-03 00:00:00.000
3 方便面 50 2013-01-03 00:00:00.000
4 矿泉水 80 2013-01-04 00:00:00.000
5 方便面 50 2013-01-05 00:00:00.000
(5 行受影响)
ID 品名 入库数量 入库时间
----------- ------ ----------- -----------------------
(0 行受影响)
*/
13、说明:选择从2到4的记录