62,046
社区成员
发帖
与我相关
我的任务
分享
select * from register r where (select count(*) from gift where userid=r.userid and type=1 and state='ok')=9
or (select count(*) from gift where userid=r.userid and type=2 and state='ok')=9
or (select count(*) from gift where userid=r.userid and type=3 and state='ok')=9
or (select count(*) from gift where userid=r.userid and type=4 and state='ok')=9
SELECT * FROM [register] WHERE [userid] in
(
SELECT [userid] FROM [gift]
WHERE [state] = 'ok'
GROUP BY [type], [userid]
HAVING COUNT(1) = 9
)
CREATE TABLE register (id int,username varchar(20))
INSERT INTO register
SELECT '1','A' union all
SELECT '2','B' union all
SELECT '3','C' union all
SELECT '4','D'
CREATE TABLE gift (id int,userid int,type int,giftname varchar(20),state varchar(10))
INSERT INTO gift
SELECT '1','1','1','gift##','ok' union all
SELECT '2','1','1','gift##','ok' union all
SELECT '3','1','1','gift##','ok' union all
SELECT '4','1','1','gift##','ok' union all
SELECT '5','1','1','gift##','ok' union all
SELECT '6','1','1','gift##','ok' union all
SELECT '7','1','1','gift##','ok' union all
SELECT '8','1','1','gift##','ok' union all
SELECT '9','1','1','gift##','ok' union all
SELECT '10','2','2','gift##','ok' union all
SELECT '11','2','3','gift##','ok' union all
SELECT '12','3','4','gift##','ok'
select r.id,r.username,g.type,count(*) as '已收集礼物' from register r left join gift g on r.id=g.userid where g.state='ok'
group by r.id,r.username,g.type
drop table register
drop table gift
select r.id,r.username,g.type from register r left join gift g on r.id=g.userid
group by r.id,r.username,g.type having [state]='ok' and count(*)=9
select r.id,r.username,g.type from register r left join gift g on r.id=g.userid
group by r.id,r.username,g.type having state='ok'
select register.* from register
inner join (
select userid from gift
where state='ok'
group by userid , type having count(type)=9)A on A.userid = register.userid