【数据库】复杂查询:视图,子查询,关联子查询

news/2024/7/9 21:17:29 标签: mysql, 数据库, postgresql

5.1视图
视图:1张临时的表
优点:1.无需保存数据,节省容量;2.频繁使用的SELECT语句可以保存为视图,提高效率。
5.2创建视图

postgres=# CREATE VIEW ProductSum(product_type,cnt_product)
postgres-# AS
postgres-# SELECT product_type,COUNT(*)
postgres-# FROM Product
postgres-# GROUP BY product_type;
CREATE VIEW

注:不要忘记AS
5.4以视图创建视图

postgres=# CREATE VIEW ProductSumJim(product_type,cnt_product)
postgres-# AS
postgres-# SELECT product_type,cnt_product
postgres-# FROM ProductSum
postgres-# WHERE product_type='办公用品';
CREATE VIEW
postgres=# SELECT * FROM ProductSumJim;
 product_type | cnt_product
--------------+-------------
 办公用品     |           2
(1 行记录)

注:1.定义视图时不要使用ORDER BY语句;2.视图可以更新的条件,视图和原表要保证同步,例如汇总后得到的视图不可以更新
5.5 可以向视图添加数据的情况

postgres=# CREATE VIEW ProductJim(product_id,product_name,product_type,sale_price,purchase_price,regist_date)
postgres-# AS
postgres-# SELECT * FROM Product
postgres-# WHERE product_type='办公用品';
CREATE VIEW
postgres=# INSERT INTO ProductJim VALUES('0009','印章','办公用品',95,10,'2009-11-30');
INSERT 0 1

5.7 删除视图

postgres=# DROP VIEW ProductSum;
错误:  无法删除 视图 productsum 因为有其它对象倚赖它
描述:  视图 productsumjim 倚赖于 视图 productsum
提示:  使用 DROP .. CASCADE 把倚赖对象一并删除.
postgres=# DROP VIEW ProductSum CASCADE;
注意:  递归删除 视图 productsumjim
DROP VIEW

5.2 子查询
子查询:一次性视图
5.9 子查询

postgres=# SELECT product_type,cnt_product
postgres-# FROM(SELECT product_type,COUNT(*) AS cnt_product
postgres(# FROM Product
postgres(# GROUP BY product_type) AS ProductSum;
 product_type | cnt_product
--------------+-------------
 衣服         |           2
 办公用品     |           2
 厨房用具     |           4
(3 行记录)

5.10 子循环可嵌套

postgres=# SELECT product_type,cnt_product
postgres-# FROM (SELECT *
postgres(# FROM (SELECT product_type,COUNT(*) AS cnt_product
postgres(# FROM Product
postgres(# GROUP BY product_type) AS ProductSum
postgres(# WHERE cnt_product=4) AS ProductSum2;
 product_type | cnt_product
--------------+-------------
 厨房用具     |           4
(1 行记录)

注:第三行括号前的空格

标量子查询:返回一行一列的数据
5.12 选出销售单价高于平均单价的情况

postgres=# SELECT product_name,sale_price
postgres-# FROM Product
postgres-# WHERE sale_price>(SELECT AVG(sale_price) FROM Product);
 product_name | sale_price
--------------+------------
 运动T恤      |       4000
 菜刀         |       3000
 高压锅       |       6800
(3 行记录)

注:WHERE不能使用聚合函数
5.13 在SELECT子句中使用标量子查询

postgres=# SELECT product_id,
postgres-# product_name,sale_price,(SELECT AVG(sale_price) FROM Product) AS avg_price
postgres-# FROM Product;
 product_id | product_name | sale_price |       avg_price     
------------+--------------+------------+-----------------------
 0001       | T恤          |       1000 | 2097.5000000000000000
 0002       | 打孔器       |        500 | 2097.5000000000000000
 0003       | 运动T恤      |       4000 | 2097.5000000000000000
 0004       | 菜刀         |       3000 | 2097.5000000000000000
 0005       | 高压锅       |       6800 | 2097.5000000000000000
 0006       | 叉子         |        500 | 2097.5000000000000000
 0007       | 擦菜板       |        880 | 2097.5000000000000000
 0008       | 圆珠笔       |        100 | 2097.5000000000000000
(8 行记录)

5.3 关联子查询
?5.16 通过关联子查询寻找每个种类中大于本类平均单价的项目

postgres=# SELECT product_type,product_name,sale_price
postgres-# FROM Product AS P1
postgres-# WHERE sale_price>(SELECT AVG(sale_price)
postgres(# FROM Product AS P2
postgres(# WHERE P1.product_type=P2.product_type
postgres(# GROUP BY product_type);
 product_type | product_name | sale_price
--------------+--------------+------------
 办公用品     | 打孔器       |        500
 衣服         | 运动T恤      |       4000
 厨房用具     | 菜刀         |       3000
 厨房用具     | 高压锅       |       6800
(4 行记录)

练习:
?5-4


http://www.niftyadmin.cn/n/1757246.html

相关文章

上下文切换

简介 单核处理器也支持多线程执行代码,CPU通过给每个线程分配CPU时间片来实现这个机制。 时间片是CPU分配给各个线程的时间,因为时间片非常短,所以CPU通过不停地切换线程执行,让我们感觉多个线程是同时执行的,时间片…

【java笔记】异常:throws,try catch

1.异常 error:不可处理 exception:可处理 (1)Runtime exception编译的时候不报错 (2)非Runtime exception不能通过编译 jvmjava虚拟机对异常的默认处理方案,找到异常的原因和位置,输…

磕代码Java:求三位数中质数的个数

方法考察&#xff1a;for循环&#xff0c;break 思路&#xff1a;用求模运算找到合数的个数。 public class Main{public static void main(String[]args){int count0;for(int i100;i<1000;i){for(int j2;j<i;j){if(i%j0){count;break;}}}System.out.println(900-count)…

聊聊2017 OWASP Top 10

关于OWASP Top10 OWASP项目最具权威的就是其“十大安全漏洞列表”&#xff08;OWASPTop 10&#xff09;&#xff0c;OWASP Top 10不是官方文档或标准&#xff0c;而只是一个被广泛采用的意识文档&#xff0c;被用来分类网络安全漏洞的严重程度&#xff0c;目前被许多漏洞奖励平…

【数据库】函数,谓词(LIKE,BETWEEN,IN),CASE

6.1函数 算术函数 6.1 创建表格&#xff1a; postgres# CREATE TABLE SampleMath postgres-# (m NUMERIC (10,3), postgres(# n INTEGER, postgres(# p INTEGER); CREATE TABLE postgres# BEGIN TRANSACTION; BEGIN postgres# INSERT INTO SampleMath(m, n, p) VALUES (500, …

【java笔记】集合List,ArrayList,LinkedList

集合分类 collection&#xff1a;单列 &#xff08;1&#xff09;List可重复 ArrayList &#xff08;2&#xff09;Set不可重复 Map&#xff1a;双列 HashMap 1.3 集合的概述和使用 package demo06Collection;import java.util.Collection; import java.util.ArrayList;pub…

为什么电脑的时间总是快2分钟

由于工作需要&#xff0c;今天领到一台新的笔记本&#xff0c; 轻轻地抚摸"新伙伴"的同时&#xff0c;发现笔记本的时间&#xff0c;快了2分钟&#xff0c;o((⊙﹏⊙))o表情~~ 明明已经联网了&#xff0c;为啥还是快两分钟呢&#xff1f; 于是我就一顿操作猛如虎&…

磕代码Java:判断上三角矩阵

思路&#xff1a; 两个for循环&#xff0c;第1个for循环次数是矩阵的行数&#xff0c;第二个循环筛选元素&#xff0c;第二个for循环前面加判断条件为0的元素位置。 import java.io.*; public class Main{public static void main(String[]args)throws IOException{BufferedRe…