postgres + with...as()

  1. 简介
    “WITH AS” 是 SQL 语言中的一个语法结构,它允许你在查询中创建一个临时的命名查询(也称为 “公共表表达式” 或 “CTE”),并可以在后续的查询中引用它。这个语法结构通常用于简化复杂查询,提高代码的可读性和可维护性。
    需要注意"WITH AS" 创建的临时表只在当前会话中有效,好处是不需要永久行临时表的权限

  2. 多个with as

WITH tmp_p AS (
	select ca.province_id, ca.province_name from crm_areas ca 
	where ca.city_id is null and ca.county_id is null
), 
tmp_up as (
SELECT province_id, count(id) cid
FROM crm_users
GROUP BY province_id
)
SELECT tmp_p.*
FROM tmp_p where tmp_p.province_id not in (select province_id from tmp_up);
  1. 结合update
WITH tmp_p AS (
	select ca.province_id, ca.province_name from crm_areas ca 
	where ca.city_id is null and ca.county_id is null
)
update crm_users set province = tmp_p.province_name from tmp_p
where crm_users.province = '' and crm_users.province_id = tmp_p.province_id;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Achieve awesome user experiences and performance with simple, maintainable code! Embrace the full stack of web development, from styling with Bootstrap, building an interactive user interface with Angular 2, to storing data quickly and reliably in PostgreSQL. With this fully revised new edition, take a holistic view of full-stack development to create usable, high-performing applications with Rails 5. Rails is a great tool for building web applications, but it's not the best at everything. Embrace the features built into your database. Learn how to use front-end frameworks. Seize the power of the application stack through Angular 2, Bootstrap, and PostgreSQL. When used together, these powerful and easy-to-use tools will open you to a new world of possibilities. This second edition is updated to cover Angular 2 - a completely reworked front-end framework - and dives into new Postgres 9.5 features such as UPSERT. Also new is Webpack coverage, to develop the front-end code for your Rails application. Create a usable and attractive login form using Bootstrap's styles, while ensuring the database table backing it is secure using Postgres' check constraints. See how creating an advanced Postgres index for a case-insensitive search speeds up your back end - enabling you to create a dynamic user experience using Angular 2. Create reusable components that bring Bootstrap and Angular together and effectively use materialized views for caching within Postgres. Get your front end working with Webpack, use Postgres' features from migrations, and write unit tests for all of it. All of this within Rails 5. You'll gain the confidence to work at every level of the application stack, bringing the right solution to every problem. What You Need: This book covers Postgres 9.5, Rails 5, and Ruby 2.3. You should have some experience with basic Rails concepts and a cursory understanding of JavaScript, CSS, and SQL, but by no means need to be an expert. You'll learn how to install Postgres on your computer or use a free version of it in the cloud. Table of Contents Chapter 1. Set Up the Environment Chapter 2. Create a Great-Looking Login with Bootstrap and Devise Chapter 3. Secure the User Database with Postgres Constraints Chapter 4. Perform Fast Queries with Advanced Postgres Indexes Chapter 5. Create Clean Search Results with Bootstrap Components Chapter 6. Build a Dynamic UI with AngularJS Chapter 7. Test This Fancy New Code Chapter 8. Create a Single-Page App Using Angular's Router Chapter 9. Design Great UIs with Bootstrap's Grid and Components Chapter 10. Cache Complex Queries Using Materialized Views Chapter 11. Asynchronously Load Data from Many Sources Chapter 12. Wrangle Forms and Validations with Angular Chapter 13. Dig Deeper Appendix A1. Full Listing of Customer Detail Page HTML Appendix A2. Creating Customer Address Seed Data Appendix A3. How Webpack Affects Deployment
PostgreSQL中的递归查询可以通过使用WITH RECURSIVE语句来实现。这种查询方式可以用于解决需要多层循环嵌套的问题。下面是一个示例: ```sql WITH RECURSIVE recursive_query AS ( -- 初始查询 SELECT initial_query UNION ALL -- 递归查询 SELECT recursive_query FROM recursive_query, recursive_table WHERE recursive_condition ) -- 最终查询 SELECT final_query FROM recursive_query ``` 在上面的示例中,WITH RECURSIVE语句定义了一个递归查询,其中包含初始查询和递归查询两部分。初始查询用于指定递归查询的起始条件,递归查询用于指定递归的条件和操作。最终查询用于从递归查询结果中选择所需的数据。 请注意,递归查询中的recursive_table是递归查询的表,recursive_condition是递归查询的条件。这些可以根据实际需求进行修改。 下面是一个具体的示例,假设有一个地区表region,其中包含地区的id和父地区的id。我们要查询某个地区的所有子地区,可以使用递归查询来实现: ```sql WITH RECURSIVE recursive_query AS ( -- 初始查询 SELECT id, name FROM region WHERE id = 1 -- 假设要查询id为1的地区的所有子地区 UNION ALL -- 递归查询 SELECT region.id, region.name FROM recursive_query, region WHERE region.parent_id = recursive_query.id ) -- 最终查询 SELECT * FROM recursive_query ``` 上面的示例中,初始查询选择了id为1的地区,递归查询选择了所有父地区id等于初始查询结果中id的地区。最终查询选择了递归查询的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值