您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

mysql 查询结果取交集的方法

2024/3/15 11:27:09发布19次查看
本文将详细介绍mysql中如何实现以sql查询返回的结果集取交集的实现方法,需要的朋友可以参考
1
mysql中如何实现以下sql查询
(select s.name
from student s, transcript t
where s.studid = t.studid and t.crscode = 'cs305')
intersect
(select s.name
from student s, transcript t
where s.studid = t.studid and t.crscode = 'cs315')
请各位不吝赐教,小弟先谢过~
解:
取交集
select a.* from
(
select s.name
from student s, transcript t
where s.studid = t.studid and t.crscode = 'cs305'
) as a
cross join
(
select s.name
from student s, transcript t
where s.studid = t.studid and t.crscode = 'cs315'
) as b on a.name = b.name;
2.
select * from (
select distinct col1 from t1 where...
union all
select distinct col1 from t1 where...
) as tbl
group by tbl.col1 having count(*) = 2
3.
交集:
select * from table1 as a join table2 as b on a.name =b.name
举例:
表a:
fielda
001
002
003
表b:
fielda
001
002
003
004
请教如何才能得出以下结果集,即表a, b行交集
fielda
001
002
003
答案:select a.fielda from a inner join b on a.fielda=b.fielda
差集:
not in 表示差集
select * from table1 where name not in (select name from table2)
以上就是mysql 查询结果取交集的方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product