ToB企服应用市场:ToB评测及商务社交产业平台

标题: 读SQL进阶教程笔记04_集合运算 [打印本页]

作者: 饭宝    时间: 2023-4-4 14:33
标题: 读SQL进阶教程笔记04_集合运算

1. 集合论是SQL语言的根基

1.1. UNION
1.2. NTERSECT和EXCEPT
1.3. 除法运算(DIVIDE BY)
2. 注意事项

2.1. SQL能操作具有重复行的集合,可以通过可选项ALL来支持
2.2. 集合运算符有优先级
2.3. 各个DBMS提供商在集合运算的实现程度上参差不齐
2.4. 除法运算没有标准定义
3. 检查集合相等性

3.1. “相等”指的是行数和列数以及内容都相同
3.2. “是同一个集合”
3.3. 原理1
3.4. 示例1
  1.   SELECT COUNT(*) AS row_cnt
  2.      FROM ( SELECT *
  3.              FROM tbl_A
  4.            UNION
  5.            SELECT *
  6.              FROM tbl_B ) TMP;
复制代码
3.5. 原理2
3.6. 示例2
  1.    SELECT CASE WHEN COUNT(*) = 0
  2.                THEN ’相等’
  3.                ELSE’不相等’END AS result
  4.      FROM ((SELECT * FROM  tbl_A
  5.            UNION
  6.            SELECT * FROM  tbl_B)
  7.            EXCEPT
  8.            (SELECT * FROM  tbl_A
  9.            INTERSECT
  10.            SELECT * FROM  tbl_B)) TMP;
复制代码
3.7. 示例3
  1.    (SELECT * FROM  tbl_A
  2.     EXCEPT
  3.     SELECT * FROM  tbl_B)
  4.    UNION ALL
  5.    (SELECT * FROM  tbl_B
  6.     EXCEPT
  7.     SELECT * FROM  tbl_A);
复制代码
4. 用差集实现关系除法运算

4.1. 嵌套使用NOT EXISTS
4.2. 使用HAVING子句转换成一对一关系
4.3. 把除法变成减法


  1.    SELECT DISTINCT emp
  2.      FROM EmpSkills ES1
  3.     WHERE NOT EXISTS
  4.            (SELECT skill
  5.              FROM Skills
  6.            EXCEPT
  7.            SELECT skill
  8.              FROM EmpSkills ES2
  9.              WHERE ES1.emp = ES2.emp);
复制代码
5. 寻找相等的子集

5.1. IBM过去研制的第一个关系数据库实验系统——System R
  1. SELECT 'A CONTAINS B'
  2.      FROM SupParts
  3.     WHERE (SELECT part
  4.              FROM SupParts
  5.            WHERE  sup ='A')
  6.              CONTAINS
  7.                  (SELECT part
  8.                    FROM SupParts
  9.                    WHERE  sup ='B')
复制代码
5.2. 示例

  1.    SELECT SP1.sup AS s1, SP2.sup AS s2
  2.      FROM SupParts SP1, SupParts SP2
  3.     WHERE SP1.sup < SP2.sup
  4.     GROUP BY SP1.sup, SP2.sup;
复制代码
  1. SELECT SP1.sup AS s1, SP2.sup AS s2
  2.      FROM SupParts SP1, SupParts SP2
  3.     WHERE SP1.sup < SP2.sup                  --生成供应商的全部组合
  4.      AND SP1.part = SP2.part                --条件1:经营同种类型的零件
  5.     GROUP BY SP1.sup, SP2.sup
  6.    HAVING COUNT(*) = (SELECT COUNT(*)       --条件2:经营的零件种类数相同
  7.                        FROM SupParts SP3
  8.                        WHERE SP3.sup = SP1.sup)
  9.      AND COUNT(*) = (SELECT COUNT(*)
  10.                        FROM SupParts SP4
  11.                        WHERE SP4.sup = SP2.sup);
复制代码
6. 用于删除重复行的高效SQL

6.1. --删除重复行:使用关联子查询
  1.    DELETE FROM Products
  2.     WHERE rowid < ( SELECT MAX(P2.rowid)
  3.                      FROM Products P2
  4.                      WHERE Products.name  = P2. name
  5.                        AND Products.price = P2.price ) ;
复制代码
6.2. --用于删除重复行的高效SQL语句(1):通过EXCEPT求补集
  1.    DELETE FROM Products
  2.     WHERE rowid IN ( SELECT rowid           --全部rowid
  3.                        FROM Products
  4.                      EXCEPT                 --减去
  5.                      SELECT MAX(rowid)     --要留下的rowid
  6.                        FROM Products
  7.                      GROUP BY name, price) ;
复制代码
6.3. --删除重复行的高效SQL语句(2):通过NOT IN求补集
  1.    DELETE FROM Products
  2.     WHERE rowid NOT IN ( SELECT MAX(rowid)
  3.                            FROM Products
  4.                          GROUP BY name, price);
复制代码
6.4. 实现了行ID的数据库只有Oracle和PostgreSQL

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4