MySQL 查詢成果取交集的完成辦法。本站提示廣大學習愛好者:(MySQL 查詢成果取交集的完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL 查詢成果取交集的完成辦法正文
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)