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 按时间段查询数据方法(实例说明)mysql如何查询某一时间段内没有卖出的商品mysql 按照时间段来获取数据的方法清空mysql 查询缓存的可行方法MySQL查询随机数据的4种方法和性能对比mysql随机查询若干条数据的方法MySql实现跨表查询的方法详解Mysql中分页查询的两个解决方法比较记录mysql性能查询过程的使用方法mysql嵌套查询和联表查询优化方法MySQL大表中重复字段的高效率查询方法MySql查询时间段的方法