如何找出相同的记录?请给出完整的SQL语句。

yehuazi 2001-09-25 06:08:40
数据表中有A、B、C、D、E、F、G字段,需要把所有A、B、C、D字段相同的记录取出。
例:
A B C D E F G
1 1 1 1 1 1 2
1 1 1 1 2 3 4
1 1 1 1 2 3 4
1 1 1 2 1 1 1
1 1 1 1 1 5 6
1 2 1 3 1 1 1
符合条件的记录:

1 1 1 1 1 1 2
1 1 1 1 1 5 6
1 1 1 1 2 3 4
1 1 1 1 2 3 4
请给出完整的SQL语句。





...全文
829 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
三个光 2001-09-29
  • 打赏
  • 举报
回复
不好意思,我理解错了
三个光 2001-09-29
  • 打赏
  • 举报
回复
我是初学sql的,还不明白group怎么用
我直接想到的方法是:
select * from tablename
where a=b and a=c and a=d and b=c and b=d and c=d
也作过试验了,也行啊
我想问问,缺点是什么?为什么要用子select?
我问这个问题是不是太傻了?
lianghu 2001-09-28
  • 打赏
  • 举报
回复
csdn真是个好地方,有这么多热心的高手!
icevi 2001-09-28
  • 打赏
  • 举报
回复
同意楼上的,不过我写的是标准的SQL,在什么上都可以用。
linzhuohao 2001-09-28
  • 打赏
  • 举报
回复
好象这个表设计得有问题吧,不合第X范式,怎么这么多重复的,
呵呵,瞎说,见笑
tpProgramer 2001-09-27
  • 打赏
  • 举报
回复
根据SQL Server文档,使用join比使用where效率更高一些.
EdwinYeah 2001-09-27
  • 打赏
  • 举报
回复
>>回复人: icevi(按钮工厂) (2001-9-25 19:25:31) 得0分
>>我知道你的意思了,就是两条记录,若ABCD都相等就将两条记录都选出来.

>>下面的语句如何:
>>select *
>>from tablename,
>> (select a,b,c,d from tablename group by a,b,c,d having count(*) >1) t1
>>where tablename.a=t1.1 and tablename.b=t1.b and tablename.c=t1.c
>> and tablename.d=t1.d


想问问大家,下面这句和icevi的有什么不同?
select *
from tablename inner join
(select a,b,c,d from tablename group by a,b,c,d having count(*) >1) t1
on tablename.a=t1.1 and tablename.b=t1.b and tablename.c=t1.c
and tablename.d=t1.d

yehuazi 2001-09-27
  • 打赏
  • 举报
回复
给分
yehuazi 2001-09-27
  • 打赏
  • 举报
回复
谢谢大家,希望以后有问题时还能得到你们的帮助。
yehuazi 2001-09-26
  • 打赏
  • 举报
回复
感谢大家的帮忙特别是icevi(按钮工厂) Sunny21cn(毛头) LUJUN(陆天),我的问题解决了。我又给贴子加了分。我想问一下你们是怎样学习SQL语句的,能提供给我一些有价值的资料吗?
icevi 2001-09-26
  • 打赏
  • 举报
回复
学习一下集合论、数据库系统原理等其实很有帮助的。
记得上大学时老师出的查询题,不是要我们写SQL 语句,而是用集合的运算来做。其实当时已经学过标准的SQL 语句的了,但老师不是强调用SQL来写,而是要我们用数学的方法来思考,所以以后再用起SQL来就比 较容易理解。关系型数据库操作是有很严格的数学理论做基础的,现在想起来当时学的有些东西真的是很有用。

不过看理论书很枯燥的, 实在不想看也可以, 那就多练练SQL吧。

我原来在学校学过SQL语句,但没用过。后来知道FOXPRO 2。6中也支持SQL查询,就开始用SQL语句了。本来我对FOXPRO中的命令也是很熟的,但编程时总坚持用SQL来做。记得我有一本FOXPRO的书,其中只有三页是讲select 语句的, 这三页后来就成了全书最脏的三页(被我翻得太多)。

SQL语法简单,但很精炼,一定要多多去用,多多实践。

Sunny21cn 2001-09-26
  • 打赏
  • 举报
回复
我并没有专门学习SQL,不过我用PowerBuilder做客户端开发,经常接触sql,为了满足客户的需求,往往要自己想一些头痛的sql语句。没有看过什么资料,大多是查看联机帮助。
我认为学习sql,首先要熟悉基本的sql语句,理解其中的原理。sql语句总共不过那么几条,所谓复杂的sql语句是这些语句的组合。其次,就是遇到问题时要多想,多讨论。其他的我就说不出来了。
我曾经碰到过类似的问题,自己想了半天,总算想出来了,就是我给你贴的那个。这次看到icevi(按钮工厂)、LUJUN(陆天)的做法,要比我好的多,确实佩服,希望这两位能多谈一些心得。谢谢。
LUJUN 2001-09-25
  • 打赏
  • 举报
回复
select * from tablename join
(select a,b,c,d from tablename group by a,b,c,d having count(*) >1) tt
on tablename.a=tt.a and tablename.b=tt.b and tablename.c=tt.c and tablename.d=tt.d order by 1,2,3,4
LUJUN 2001-09-25
  • 打赏
  • 举报
回复
select * from tablename join
(select a,b,c,d from tablename group by a,b,c,d having count(*) >1) tt
on tablename.a=tt.a and tablename.b=tt.b and tablename.c=tt.c and tablename.d=tt.d
Sunny21cn 2001-09-25
  • 打赏
  • 举报
回复
icevi(按钮工厂):
我测试的时间长点儿了,没想到老兄写好了,不错不错,佩服佩服
交个朋友?
OICQ: 33638458
google_real 2001-09-25
  • 打赏
  • 举报
回复
maybe the follow sql statement will give a better performance:

select * from tablename t
where exists(select 1 from tablename
where a=t.a and b=t.b and c=t.c and d=t.d
group by a,b,c,d having count(*) > 1)
KingSunSha 2001-09-25
  • 打赏
  • 举报
回复
select * from T1
where (a,b,c,d) in (select a,b,c,d
from (select a,b,c,d,count(*)
from t1
group by a,b,c,d
having count(*) > 1)
)
google_real 2001-09-25
  • 打赏
  • 举报
回复
agree with icevi
Sunny21cn 2001-09-25
  • 打赏
  • 举报
回复
select * from T1
where convert( char(1), A ) + '!' +
convert( char(1), B ) + '!' +
convert( char(1), C ) + '!' +
convert( char(1), D ) + '!'
in(
select convert( char(1), A ) + '!' +
convert( char(1), B ) + '!' +
convert( char(1), C ) + '!' +
convert( char(1), D ) + '!'
from T1
group by A, B, C, D
having count(*) > 1
)

如果你的字段是字符型的,不需要做convert。
‘!’用作分隔符,根据情况,可以不加。

20分少了,赶快给分!!!
icevi 2001-09-25
  • 打赏
  • 举报
回复
我知道你的意思了,就是两条记录,若ABCD都相等就将两条记录都选出来.

下面的语句如何:
select *
from tablename,
(select a,b,c,d from tablename group by a,b,c,d having count(*) >1) t1
where tablename.a=t1.1 and tablename.b=t1.b and tablename.c=t1.c
and tablename.d=t1.d
加载更多回复(6)

34,590

社区成员

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

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