postgres + with...as()

编程入门 行业动态 更新时间:2024-10-09 17:22:25

<a href=https://www.elefans.com/category/jswz/34/1770997.html style=postgres + with...as()"/>

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;

更多推荐

postgres + with...as()

本文发布于:2023-11-17 06:52:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638220.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:postgres

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!