Mysql学习MySQL 查询结果取交集的实现方法
发布时间:2020-12-12 01:26:02 所属栏目:MySql教程 来源:网络整理
导读:《Mysql学习MySQL 查询结果取交集的实现方法》要点: 本文介绍了Mysql学习MySQL 查询结果取交集的实现方法,希望对您有用。如果有疑问,可以联系我们。 1 MySQL中如何实现以下SQL查询 (SELECT S.Name FROM STUDENT S,TRANSCRIPT T WHERE S.StudId = T.StudId
《Mysql学习MySQL 查询结果取交集的实现方法》要点: 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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |