12 回答
TA贡献1863条经验 获得超2个赞
select unamefrom dbo.UserInfowhere uid in ( select uid from (select count(1) as pcount,uid from (select distinct uid,pid from Buyinfo) a group by uid) b inner join (select count(1) as pcount from Product) c on c.pcount = b.pcount)
TA贡献1725条经验 获得超7个赞
提供个思路:
1) 将购买信息表根据Uid进行分组,同时去除重复购买信息
2) 将上述结果Count下和产品表的Count对比,相等则说明用户购买了所有商品
3) 最后将用户信息表的uid和第二步结果的uid对比
select u.uid from UserInfo as u where u.uid = (select uid from buyinfo as b where uid=(select top 1 uid from buyinfo where uid=b.uid) group by uid having Count(uid)= (select Count(pid) from product))
TA贡献1856条经验 获得超11个赞
但是你的语句有问题
最后综合了
select uname from UserInfo u join(
select uid from (select uid,pid from Buyinfo group by uid,pid)a group by uid having count(pid)=(select count(1)from Product))tu
on u.uid=tu.uid
TA贡献1856条经验 获得超17个赞
看看我这个
SELECT u.uid '用户id',u.uname '用户' FROM userinfo u
INNER JOIN
(SELECT uid FROM buyinfo
GROUP BY uid
HAVING COUNT(DISTINCT pid)=(SELECT COUNT(pid)from product)) AS f
ON u.uid=f.uid
- 12 回答
- 0 关注
- 667 浏览
添加回答
举报