在一个 postgresql 查询中使用多个 with 语句
with
语句用于创建临时表,这意味着这些表不会持久化在数据库中,并且只存在于内存中,直到查询完成。
引入了 with
语句以将复杂查询分解为更易于处理和调试的简单查询。
本篇文章将教授如何使用多个 with
语句在 postgresql 中使用两个临时表执行查询。
在一个 postgresql 查询中使用多个 with
语句
使用以下命令登录到你的 postgresql 数据库。默认用户是 postgres
。
如果数据库中有多个用户,请更改用户名。如果你在登录期间配置了用户身份验证,则在下一个提示中输入密码。
david@david-hp-probook-6470b:~$psql-upostgres
成功登录 postgresql 服务器后,使用以下命令创建并连接到我们将用于存储数据的数据库。
postgres=#createdatabasemultiple_with_db;createdatabasepostgres=#\cmultiple_with_db;youarenowconnectedtodatabase"multiple_with_db"asuser"postgres".
我们首先需要创建两个持久表,我们将从中创建临时表。第一个表将保存客户数据。
创建 customer
表,如以下数据定义语言所示。要创建表格,你可以将查询复制并粘贴到终端上,然后按 enter。
multiple_with_db=#createtablecustomer(customer_idserialuniquenotnull,first_namevarchar(50),last_namevarchar(50),emailvarchar(60),primarykey(customer_id));createtable
使用以下数据操作语言在 customer
表中创建一些客户。你可以在终端上复制并粘贴查询以将记录插入表中。
multiple_with_db=#insertintocustomer(first_name,last_name,email)values('john','doe','john@gmail.com');insert01multiple_with_db=#insertintocustomer(first_name,last_name,email)values('mary','public','mary@gmail.com');insert01multiple_with_db=#insertintocustomer(first_name,last_name,email)values('peter','parker','peter@gmail.com');insert01multiple_with_db=#insertintocustomer(first_name,last_name,email)values('steve','harvey','steve@gmail.com');insert01
使用以下查询来验证你的记录是否已成功创建。
multiple_with_db=#select*fromcustomer;
输出:
customer_id | first_name | last_name | email
------------- ------------ ----------- -----------------
1 | john | doe | john@gmail.com
2 | mary | public | mary@gmail.com
3 | peter | parker | peter@gmail.com
4 | steve | harvey | steve@gmail.com
(4 rows)
第二个表包含客户购买产品的订单信息。创建 customer_order
表,如下所示。
multiple_with_db=#createtablecustomer_order(order_idserialuniquenotnull,product_namevarchar(50),product_priceinteger,product_quantityinteger,total_priceinteger,created_atdate,cust_idintegerreferencescustomer(customer_id));createtable
将一些记录插入 customer_order
表并确保引用完整性约束引用客户,如下所示。
multiple_with_db=#insertintocustomer_order(product_name,product_price,product_quantity,total_price,created_at,cust_id)values('laptop',500,3,3*500,'2022-03-07',1);insert01multiple_with_db=#insertintocustomer_order(product_name,product_price,product_quantity,total_price,created_at,cust_id)values('laptop',500,4,4*500,'2022-03-07',3);insert01multiple_with_db=#insertintocustomer_order(product_name,product_price,product_quantity,total_price,created_at,cust_id)values('laptop',500,7,7*500,'2022-03-07',4);insert01multiple_with_db=#insertintocustomer_order(product_name,product_price,product_quantity,total_price,created_at,cust_id)values('laptop',500,5,5*500,'2022-03-07',2);insert01
使用以下查询来确保你的数据已成功保存在数据库中。
multiple_with_db=#select*fromcustomer_order;
输出:
order_id | product_name | product_price | product_quantity | total_price | created_at | cust_id
---------- -------------- --------------- ------------------ ------------- ------------ ---------
1 | laptop | 500 | 3 | 1500 | 2022-03-07 | 1
2 | laptop | 500 | 4 | 2000 | 2022-03-07 | 3
3 | laptop | 500 | 7 | 3500 | 2022-03-07 | 4
5 | laptop | 500 | 5 | 2500 | 2022-03-07 | 2
(4 rows)
在 postgresql 中使用逗号分隔多个 with
语句
要使用多个 with
语句,第一个 with
语句后跟一个逗号 (,
) 而不是另一个 with
语句。
下面的例子展示了我们如何使用多个用逗号分隔的 with
语句来执行查询。
第一个临时表是用 customer
表中的所有数据创建的,第二个临时表是用 customer_order
表中的所有数据创建的。
在临时表上执行查询以返回两列,一列包含客户的电子邮件,另一列包含每个客户购买的产品的总价格。
multiple_with_db=#withcustomer_infoas(select*fromcustomer),order_infoas(select*fromcustomer_order)select(email,total_price)fromcustomer_infot1innerjoinorder_infot2ont1.customer_id=t2.order_id;
输出:
row
------------------------
(john@gmail.com,1500)
(mary@gmail.com,2000)
(peter@gmail.com,3500)
(steve@gmail.com,2500)
(4 rows)
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
发布时间:2023/03/20 浏览次数:185 分类:postgresql
-
本文介绍如何在 ubuntu 上找到 postgresql 数据库的配置文件。
发布时间:2023/03/20 浏览次数:409 分类:数据库
-
本文解释了如何直接从终端/命令行或 psql shell 运行 sql 文件。为此,你需要指定主机名、端口、用户名和数据库名称。
发布时间:2023/03/20 浏览次数:89 分类:postgresql
-
本文展示了如何列出 postgresql 上的活动连接。
发布时间:2023/03/20 浏览次数:966 分类:postgresql
-
在 pl/sql 中,你可能需要在 postgres 中使用循环。我们可以使用 for 和 while 语句来创建循环。
发布时间:2023/03/20 浏览次数:141 分类:postgresql
-
本文介绍如何在 postgresql 中仅使用单个查询来重命名列以及更改其类型。
发布时间:2023/03/20 浏览次数:233 分类:postgresql
-
本文介绍如何在 postgresql 中使用 select 方法连接列。
发布时间:2023/03/20 浏览次数:281 分类:postgresql
-
本文展示了如何使用 case 语句并给出了 postgresql 中的示例。
发布时间:2023/03/20 浏览次数:356 分类:postgresql
-
本文介绍如何将 postgresql 中的平均值四舍五入到小数点后 2 位。