首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

选择两列差值大于某值的行,该怎么解决

2012-02-19 
选择两列差值大于某值的行表 A列1列2id121611319205325614。。。。。。选取表A中列2-列1的差值大于5的行[解决办

选择两列差值大于某值的行
表 A
列1 列2 id
12 16 1
13 19 2
0 5 3
25 61 4
。。。
。。。
选取表A中列2-列1的差值大于5的行

[解决办法]

SQL code
 
declare @表A table (列1 int,列2 int,id int)
insert into @表A
select 12,16,1 union all
select 13,19,2 union all
select 0,5,3 union all
select 25,61,4 union all
select 20,10,5

select * from @表A where 列2-列1>5
/*
列1      列2      id
----------- ----------- -----------
13      19      2
25      61      4
*/
select * from @表A where abs(列2-列1)>5
/*
列1      列2      id
----------- ----------- -----------
13      19      2
25      61      4
20      10      5
*/

热点排行