`

复杂查询sql intersect ,union ,except

阅读更多

select * from

(select userid from bsalelog where ...) a,

(select userid from bsalelog where ...) b

.....

where a.userid = b.userid

效果和上面一样

/*求t1对t2的交集
select * from t1 intersect select * from t2

-------------------------------------------------------------------

/* 求表并集
select * from t1 union select * from t2

//求表并集不过滤重复
select * from t1 union all select * from t2

/*求t1对t2的差集
select * from t1 except select * from t2

-------------------------------------------------------------------

select userid from
(select userid,count(*) as num from
(select userid,sum(money) as money,datediff(DD,'2009-11-23',dhm) as day from bsalelog group by userid,datediff(DD,'2009-11-23',dhm)) a
where day>=0 and day<=1 and money>=100 group by userid) b where num=0

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics