查询语句判断多个字段in结果集的问题

illutomato 2016-05-10 04:39:21

Table1 --表名
C1,C2,C3,C4,C5 --字段名

select * from Table1 where (C1,C2,C3) in (select C1,C2,C3 from Table1 group by C1,C2,C3 having count(*)>1)

上面这个查询语句是想把表中C1,C2,C3重复的数据赛选出来,
语句报错
“消息 4145,级别 15,状态 1,第 1 行
在应使用条件的上下文(在 ',' 附近)中指定了非布尔类型的表达式。”


正确的该如何写呢? 高手指点下~谢谢。
...全文
966 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ginnnnnnnn 2016-05-17
  • 打赏
  • 举报
回复
Try this
;with CTE AS(
select  * ,
		Row_Number() over(partition by C1,C2,C3 order by (select 1)) as  RNr
	from table1 
)
select * from CTE a where exists(select * from CTE where C1 = a.C1 and C2 = a.C2 and C3 = a.C3 and RNr > 1)
 
illutomato 2016-05-17
  • 打赏
  • 举报
回复
引用 5 楼 sc273607742 的回复:
[quote=引用 3 楼 illutomato 的回复:] [quote=引用 1 楼 sc273607742 的回复:] select * from table1 a where exists(select 1 from table1 where c1=a.c1 and c2=a.c2 and c3=a.c3 and (c4!=a.c4 or c5!=a.c5)) 这样试试
这个是空的结果集,而且我这个表的字段很多,只是举了5个字段[/quote] delete table1 a where not exists(select 1 from table1 where a.c1=c1 and a.c2=c2 and a.c3=c3 having count(1)=1) 试试,ORACLE中能用,sqlserver没验证[/quote] 你是来混分的吗......
gw6328 2016-05-17
  • 打赏
  • 举报
回复

;WITH cte AS (
SELECT rn=ROW_NUMBER() OVER(PARTITION BY c1,c2,c3 ORDER BY GETDATE()),* FROM table1
)
SELECT * FROM cte WHERE rn>1

哥眼神纯洁不 2016-05-17
  • 打赏
  • 举报
回复
引用 6 楼 illutomato 的回复:
[quote=引用 5 楼 sc273607742 的回复:] [quote=引用 3 楼 illutomato 的回复:] [quote=引用 1 楼 sc273607742 的回复:] select * from table1 a where exists(select 1 from table1 where c1=a.c1 and c2=a.c2 and c3=a.c3 and (c4!=a.c4 or c5!=a.c5)) 这样试试
这个是空的结果集,而且我这个表的字段很多,只是举了5个字段[/quote] delete table1 a where not exists(select 1 from table1 where a.c1=c1 and a.c2=c2 and a.c3=c3 having count(1)=1) 试试,ORACLE中能用,sqlserver没验证[/quote] 你是来混分的吗......[/quote] 你是真不懂
哥眼神纯洁不 2016-05-11
  • 打赏
  • 举报
回复
引用 3 楼 illutomato 的回复:
[quote=引用 1 楼 sc273607742 的回复:] select * from table1 a where exists(select 1 from table1 where c1=a.c1 and c2=a.c2 and c3=a.c3 and (c4!=a.c4 or c5!=a.c5)) 这样试试
这个是空的结果集,而且我这个表的字段很多,只是举了5个字段[/quote] delete table1 a where not exists(select 1 from table1 where a.c1=c1 and a.c2=c2 and a.c3=c3 having count(1)=1) 试试,ORACLE中能用,sqlserver没验证
illutomato 2016-05-10
  • 打赏
  • 举报
回复
引用 2 楼 kingtiy 的回复:

SELECT a.* FROM Table1 a JOIN (
select C1,C2,C3,COUNT(1) 重复次数 FROM Table1 GROUP BY C1,C2,C3 HAVING COUNT(1)>1
) b ON a.c1=b.c1 AND a.c2=b.c2 AND a.c3=b.c3
--这样就可以找出重复的数据了.
这样可以找到重复数据,但我的最终目的是要删除这些数据,把select 改成delete 执行不了... 有什么办法吗
illutomato 2016-05-10
  • 打赏
  • 举报
回复
引用 1 楼 sc273607742 的回复:
select * from table1 a where exists(select 1 from table1 where c1=a.c1 and c2=a.c2 and c3=a.c3 and (c4!=a.c4 or c5!=a.c5)) 这样试试
这个是空的结果集,而且我这个表的字段很多,只是举了5个字段
kingtiy 2016-05-10
  • 打赏
  • 举报
回复

SELECT a.* FROM Table1 a JOIN (
select C1,C2,C3,COUNT(1) 重复次数 FROM Table1 GROUP BY C1,C2,C3 HAVING COUNT(1)>1
) b ON a.c1=b.c1 AND a.c2=b.c2 AND a.c3=b.c3
--这样就可以找出重复的数据了.
哥眼神纯洁不 2016-05-10
  • 打赏
  • 举报
回复
select * from table1 a where exists(select 1 from table1 where c1=a.c1 and c2=a.c2 and c3=a.c3 and (c4!=a.c4 or c5!=a.c5)) 这样试试

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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