求SQL语句,一个表里有相同的数据,我只要取所有不同的数据 , 来者有分

xzhangjie 2003-10-03 01:57:59
比如说一个表有两项 title, text,
由于一些原因,使表中有title相同的数据, 但我只想取出所有不同的title的数据。
title text
'aaaa' 'dddddddddddddddddd'
'aaaa' 'dddddddddddddddddd'
'bbbb' 'dfsfdfffffffffffff'

我要查询出
'aaaa' 'dddddddddddddddddd'
'bbbb' 'dfsfdfffffffffffff'

的SQL语句:
...全文
106 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
welyngj 2003-10-03
  • 打赏
  • 举报
回复
select distinct title,text from 表
txlicenhe 2003-10-03
  • 打赏
  • 举报
回复
蚂蚁的:去除重复值
如果有ID字段,就是具有唯一性的字段

delect table where id not in (

select max(id) from table group by col1,col2,col3...
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。



2,如果是判断所有字段也可以这样
select * into #aa from table group by id1,id2,....
delete table
insert into table
select * from #aa



3,没有ID的情况

select identity(int,1,1) as id,* into #temp from tabel
delect # where id not in (
select max(id) from # group by col1,col2,col3...)
delect table
inset into table(...)
select ..... from #temp


col1+','+col2+','...col5 联合主键


select * from table where col1+','+col2+','...col5 in (

select max(col1+','+col2+','...col5) from table
where having count(*)>1
group by col1,col2,col3,col4
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。

2,
select identity(int,1,1) as id,* into #temp from tabel
select * from #temp where id in (
select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)

xzhangjie 2003-10-03
  • 打赏
  • 举报
回复
不明白,比如说一个表tt有
title, text(类型text), date,author
一条记录可能被导入了多次,也就是说,有完全相同的数据,我要把不重复的数据导出,应该如何写?
pengdali 2003-10-03
  • 打赏
  • 举报
回复
如果还有其他若干个列且text不重复:

select * from 表 tem where text=(select top 1 text from 表 where title=tem.title)
pengdali 2003-10-03
  • 打赏
  • 举报
回复
select title,max(text) from 表 group by title

34,875

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧