sql中如何查看某一字段值有几个数值

如题所述

用分组,组内计数就可以了,意思就是根据字段a的取值进行分组,相同的为一组,在用count进行组内计数select a,count(*)from Agroup by a
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-04-18
CREATE proc Full_Search(@string varchar(50))
as
begin

declare @tbname varchar(50)
declare tbroy cursor for select name from sysobjects
where xtype= 'u ' --第一个遍历所有的表

open tbroy
fetch next from tbroy into @tbname
while @@fetch_status=0
begin

declare @colname varchar(50)
declare colroy cursor for select name from syscolumns
where id=object_id(@tbname) and xtype in (
select xtype from systypes
where name in ( 'varchar ', 'nvarchar ', 'char ', 'nchar ') --为字符型的字段
) --第二个游标是第一个游标的嵌套游标,遍历某个表的所有字段

open colroy
fetch next from colroy into @colname
while @@fetch_status=0
begin

declare @sql nvarchar(1000),@j int
select @sql= 'select @i=count(1) from ' +@tbname + ' where '+ @colname+ ' like '+ '''%'+@string+ '%'''
exec sp_executesql @sql,N'@i int output',@i=@j output --输出满足条件表的记录数
if @j> 0
BEGIN
select 包含的表名=@tbname
--exec( 'select distinct '+@colname+' from ' +@tbname + ' where '+ @colname+ ' like '+ '''%'+@string+ '%''')
END
fetch next from colroy into @colname
end

close colroy
deallocate colroy

fetch next from tbroy into @tbname
end
close tbroy
deallocate tbroy
end
go本回答被提问者采纳
第2个回答  2018-07-31
很简单的事情,非得回答的这么复杂干嘛?我来回答一下。

SELECT count(DISTINCT col_name) from table_name;

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网