sql语句实现一个简单的查询

问题:有张表有3个字段,如A、B、C,其中字段B有重复值,现需写SQL语句实现仅列示B字段有重复值的记录。
如原始数据集:
1,2,3
2,4,5
3,2,6
4,5,7
7,6,1
6,5,7

正确列出应为:
1,2,3
3,2,6,
4,5,7
6,5,7

第1个回答  2010-08-15
1、 先找出B字段中的数值的数量

select B字段, count(B字段) as num from 数据表 group by B字段

2、 先找出B字段中的重复值的记录值

select B字段, count(B字段) as num from 数据表 group by B字段

就是 num >1 的记录集

3、 求解

select a.A字段,a.B字段,a.C字段
from 数据表 a,
( select B字段, count(B字段) as num from 数据表 group by B字段) b
where a.B字段 = b.B字段 and b.num>1
第2个回答  2010-08-15
select A,表.B,C from test01 join
(select count(B) as Q,B from 表
group by B
having count(B)=2) as T on 表.B=T.B
表是你所创建的表名
第3个回答  2010-08-15
假设表的名字为t1:
select * from t1 where b in (select b from t1 group by b having count(b)>1)本回答被提问者采纳
第4个回答  2010-08-15
select * from 表 where B in(select B from 表 group by B having count(*)>1)
相似回答