当前位置: 代码迷 >> Sql Server >> !关于查询其中的20条数据
  详细解决方案

!关于查询其中的20条数据

热度:83   发布时间:2016-04-27 16:19:36.0
请求帮助!关于查询其中的20条数据
我的数据库中有这样一张表
id   a1   a2   a3   a4
1     1     3     6     2
2     5     7     8     9
3     3     7     8     9
4     4     3     2     1
5     6     7     5     4

表中的id为自增关键字

我现在想在最后的3行数据中搜索值为4的数据

上面的情况的查询语句写不出来
也就是如果在表中来进行一个特定范围查询

------解决方案--------------------
select *
from (select top 3 * from 表 order by id desc)t
where a1=4 or a2=4 or a3=4 or a4=4
------解决方案--------------------
declare @t table(id int, a1 int)
insert @t
select 1, 1 union all
select 2, 5 union all
select 3, 3 union all
select 4, 4 union all
select 5, 6

SELECT * FROM
(select top 3 * from @t order by id DESC) AS t
WHERE id = 4

/*结果
id a1
----------- -----------
4 4
*/
  相关解决方案