请教一下各位:
表如下:
id userid funtionName
1 1 药物专论
2 2 配伍挤
4 3 时讯
5 1 时讯
6 2 药物的相互作用
7 2 药物专论
8 3 配伍挤
问:
要显示这三列 但是funtionName不能有重复
sql语句怎么写
????
表如下:
id userid funtionName
1 1 药物专论
2 2 配伍挤
4 3 时讯
5 1 时讯
6 2 药物的相互作用
7 2 药物专论
8 3 配伍挤
问:
要显示这三列 但是funtionName不能有重复
sql语句怎么写
????
... order by functionName
slect max(id),max(userid),funtionName
from table
group by funtionName
http://topic.csdn.net/u/20100412/22/fec647ea-73d0-480b-92e9-8af61ef3c978.html
--不重复的记录有
select id,userid,funtionname from tablename --where
group by id,userid,funtionname
having count(funtionname)=1
--重复的记录
select id,userid,funtionname from tablename --where
group by id,userid,funtionname
having count(funtionname)>1
--> 数据库版本:
--> Microsoft SQL Server 2008 (RTM) - 10.0.1600.22
--> 测试数据:[TB]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[TB]')
AND type in (N'U'))
DROP TABLE [TB]
GO---->建表
create table [TB]([id] int,[userid] int,[funtionName] varchar(14))
insert [TB]
select 1,1,'药物专论' union all
select 2,2,'配伍挤' union all
select 4,3,'时讯' union all
select 5,1,'时讯' union all
select 6,2,'药物的相互作用' union all
select 7,2,'药物专论' union all
select 8,3,'配伍挤'
GO
--> 查询结果
alter table TB add dd varchar(20)
godeclare @i int,@fff varchar(20)=''
set @i=1
update [TB]
set dd=@i, @i=case when [funtionName]!=@fff then 0 else @i+1 end ,@fff=[funtionName]
goSELECT * FROM [TB]
where dd=0alter table TB drop column dd
--> 删除表格
--DROP TABLE [TB]
这样就可以了,简单的很,其它楼主你应该表明,是funtionName重要,还是UserID重要,按我的理解,是funtionName重要,至于相同的funtionName取什么UserID,楼主有什么要求吗?
上面的语句是,相同的funtionName取UserID大的记录
select * from table
where id in (select min(id) from table group by funtionName)
要显示3列,用 group / distinct 都没用,因为 id 是不会重复的所以只能先对 funtionName group
其他2个字段只能用表达式了
参见一篇MySql的贴子应该有借鉴作用:
查询不重复记录
group by id,userid,funtionname
having count(funtionname)=1
count(funtionname)=2 的显示否?
select id,userid,funtionName from 表 group by funtionName