mysql并集_mysql取差集、交集、并集
[在实现数据挖掘⼀些算法或者是利⽤空间向量模型来发现相似⽂档的时候,会遇到求两个集合的交集的情况。以前⼀般是采⽤⽐较笨拙的办法,依次遍历其中⼀个集 合的元素,然后
需求:从两个不同的结果集(⼀个是⼦集,⼀个是⽗集),[SELECT ID FROM ( SELECT DISTINCT A.ID AS ID FROM TABLEA A #有ID: 1 2 3 4 5 UNION ALL SELECT DISTINCT B.ID AS ID FROM TABLEB B #有ID: 2 3字段为电话号码phone_number,出⽗集中缺少的电话号码,以明确⽤户⾝份。
结合⽹上资料,整理sql如下:
//
mysql取差集
select num FROM
(
de_sn as id,u.phone_number as num from t1 b
left join t2 u
on b.from_de_sn
_account='1000' and b.json_info like '%09⽉份%'
union all
de_sn as id,u.phone_number as num from t3 o
left join t2 u
on o.buyer_code_de_sn
where o.seller_account_code_sn='1000' and o.json_info like '%09⽉份%'
)t3
group by num having count(num)=1
//
mysql取交集
select num FROM
(
de_sn as id,u.phone_number as num from t1 b
left join t2 u
on b.from_de_sn
_account='1000' and b.json_info like '%09⽉份%'
union all
de_sn as id,u.phone_number as num from t1 o
left join t2 u
on o.buyer_code_de_sn
where o.seller_account_code_sn='1000' and o.json_info like '%09⽉份%'
)t3
group by num having count(num)=2
//
mysql取并集
select num FROM
(
de_sn as id,u.phone_number as num from t1 b
left join t2 u
on b.from_de_sn
_account='1000' and b.json_info like '%09⽉份%'
union all
de_sn as id,u.phone_number as num from t1 o
left join t2 umysql交集查询
on o.buyer_code_de_sn
where o.seller_account_code_sn='1000' and o.json_info like '%09⽉份%'
)t3[MySQL的联结(Join)以及MySQL求交集和差集在SQL标准中规划的(Join)联结⼤致分为下⾯四种:1. 内联结:将两个表中存在联结关系的字段符合联结关系的那些记录形成记录集

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。