菜鸟求一条sql语句,在线等结贴,急啊!~~~~~

zhangsuyunpk521 2010-09-13 02:57:46
编号 产品名 数量
1 牛仔裤 1
1 外套 2
2 牛仔裤 1
2 外套 2
3 牛仔裤 3
3 外套 1
3 内衣 1
4 牛仔裤 1
5 内衣 1
.
.
.
n 内衣 1
我现在要统计出:1.只买了牛仔裤的用户编号
2.即买牛仔裤又买了内衣的用户编号
...全文
65 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwwb 2010-09-13
  • 打赏
  • 举报
回复
SELECT * from ttp1 a where exists(select 1 from ttp1 where 编号=a.编号 and 产品名='内衣') and not exists(select 1 from ttp1 where 编号=a.编号 and 产品名='外套')
and 产品名='牛仔裤'
zhangsuyunpk521 2010-09-13
  • 打赏
  • 举报
回复
第二条估计是我没有说清楚,我要的只买了牛仔裤和内衣的,买了其他的就不算!~~
zhangsuyunpk521 2010-09-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 acmain_chm 的回复:]
1.只买了牛仔裤的用户编号

SQL code
select distinct 编号 from table1 a
where 产品名='牛仔裤'
and not exists (select 1 from table1 where 编号=a.编号 and 产品名!='牛仔裤')
[/Quote]
高手能不能解释下:select 1 from table1 where 编号=a.编号 and 产品名!='牛仔裤'他啊
ACMAIN_CHM 2010-09-13
  • 打赏
  • 举报
回复
2.即买牛仔裤又买了内衣的用户编号

select distinct a.编号
from table1 a ,table1 b
where a.编号=b.编号
and a.产品名='牛仔裤' and b.产品名='内衣'
ACMAIN_CHM 2010-09-13
  • 打赏
  • 举报
回复
1.只买了牛仔裤的用户编号
select distinct 编号 from table1 a
where 产品名='牛仔裤'
and not exists (select 1 from table1 where 编号=a.编号 and 产品名!='牛仔裤')
WWWWA 2010-09-13
  • 打赏
  • 举报
回复
1
SELECT * from ttp1 a where not exists(select 1 from ttp1 where 编号=a.编号 and 产品名 in('外套','内衣'))
and 产品名='牛仔裤'

2
SELECT * from ttp1 a where exists(select 1 from ttp1 where 编号=a.编号 and 产品名 in('内衣'))
and 产品名='牛仔裤'

2,596

社区成员

发帖
与我相关
我的任务
社区描述
Sybase相关技术讨论区
社区管理员
  • Sybase社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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