`
lizhiyu211
  • 浏览: 225184 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

救命!帮忙解答一个单表自连接查询问题

 
阅读更多

在产品表中查询产品名称、规格一样的产品有哪些:
要求出的结果有:产品编号、产品名称、规格
cp(产品表):产品编号、产品名称、规格

分享到:
评论
3 楼 lizhiyu211 2009-12-03  
我做出来的答案是这样的:
   
     select distinct a.* from cp a,cp b where a.产品名称=b.产品名称 and a.规格=b.规格 and a.产品编号!=b.产品编号
    


仅供参考
2 楼 Jekey 2009-12-03  
Jekey 写道
select 产品编号,产品名称,规格 from cp where
产品名称,规格 in(select 产品名称,规格 from cp group by 产品名称,规格
having count(*)>1)

修正一下
select t1.产品编号,t1.产品名称,t1.规格 from cp t1 where
exists (select t2.产品名称,t2.规格 from cp t2
where t1.产品名称=t2.产品名称 and t1.规格=t2.规格
group by t2.产品名称,t2.规格
having count(*)>1)
1 楼 Jekey 2009-12-03  
select 产品编号,产品名称,规格 from cp where
产品名称,规格 in(select 产品名称,规格 from cp group by 产品名称,规格
having count(*)>1)

相关推荐

Global site tag (gtag.js) - Google Analytics