是这样的,我的数据表中有字段1,2,3,4,5,6,7 , 它们的值分别都是1~35的数字,我想知道1~35每个数字在字段1~5中总共出现几次,这样的SQL语句应该怎么写?
比如说字段1 字段2 字段3 字段4 字段5 字段6 字段7
1 3 5 6 16 30 33
4 5 8 9 12 34 35如果是这样的话,我要得出的是1~35 每个数字在字段1~5(总共有7个字段,但搜索范围是5个字段)中出现的次数,比如1:2次.... 5:2次 应该怎么写查询语句???在线等。
比如说字段1 字段2 字段3 字段4 字段5 字段6 字段7
1 3 5 6 16 30 33
4 5 8 9 12 34 35如果是这样的话,我要得出的是1~35 每个数字在字段1~5(总共有7个字段,但搜索范围是5个字段)中出现的次数,比如1:2次.... 5:2次 应该怎么写查询语句???在线等。
declare @ziduanhao int
declare @num int
set @ziduanhao = 1
set @num = 0
while ( @ziduanhao <5 )
begin
declare chaxun sursor for select @ziduanhao from table_name
open chaxun
fetch next from table_name INTO @data
if(@data>=1 and @data <=35)
begin
set @num=@num+1
fetch next from table_name INTO @data
end
@ziduanhao=@ziduanhao+1
select @num
end
close chaxun
deallocate chaxun
declare @data int
declare @ziduanhao int
declare @num int
set @ziduanhao = 1
set @num = 0
while ( @ziduanhao <5 )
begin
declare chaxun sursor for select @ziduanhao from table_name
open chaxun
fetch next from table_name INTO @data
if(@data >=1 and @data <=35)
begin
set @num=@num+1
fetch next from table_name INTO @data
end
select @ziduanhao,@num
set @ziduanhao = @ziduanhao+1
set @num = 0
end
close chaxun
deallocate chaxun
如果要看的数字是个范围段,比如 1-35,那直接用 between and就可以了,也就是我写的第一种写法.
如果要看的数字是零散的,不是范围段,比如 3,9,4,27 那么用第一种写法,或者用 in