数据库查询
用户订购产品,需要查询必须订购了产品1,但是产品2,3,4,5没有全部订购的用户这个需要怎么查询
servst 用户表 product产品表关联通过用户id(servid)
学习一下 本帖最后由 syg5434 于 2016-12-21 16:14 编辑
仅供参考,初学者,望多交流。
with s1 as
(select servid, count(distinct t.type) as p_count
from product t
where t.type != 1
group by servid)
select *
from servst s
where s.id in (select servid from s1 where s1.p_count between 1 and 3)
and exists (select 1
from product p
where s.id = p.servid
and p.type = 1)
页:
[1]