惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
T
Tenable Blog
Hugging Face - Blog
Hugging Face - Blog
Google Online Security Blog
Google Online Security Blog
博客园 - Franky
P
Proofpoint News Feed
H
Hacker News: Front Page
P
Privacy & Cybersecurity Law Blog
月光博客
月光博客
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
Forbes - Security
Forbes - Security
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
博客园 - 叶小钗
T
Threat Research - Cisco Blogs
aimingoo的专栏
aimingoo的专栏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
O
OpenAI News
G
Google Developers Blog
Martin Fowler
Martin Fowler
罗磊的独立博客
S
SegmentFault 最新的问题
T
Tor Project blog
量子位

PostgreSQL

五年过去了,结论有什么变化吗? 适配信创数据库的人的精神状态 be like - V2EX 用了十几年的 MySQL 了,突然发现 PostgreSQL 可能更加适合我,大家怎么看? - V2EX postgresql 多进程改多线程 - V2EX 二键安装一个 postgresql 服务 - V2EX 通过 pg_roaringbitmap 优化标签查询的话,有没有必要做高频标签映射和结合普通复合索引? - V2EX PostgreSQL + TimescaleDB 方案不按主键索引走,有什么办法吗? - V2EX 看看大家现在用 pg 的最佳实践是怎么样的 - V2EX 用 PostgreSQL 存图片等 binary 有什么坑吗 - V2EX [求助] 在哪个网址能够下载 PostgresSQL 9.3.0 版本的 Windows 二进制安装包?官方/ftp/old/binary 里没有。 - V2EX [请教]pgvector 的性能问题 - V2EX 多笔记本离线 PostgreSQL 数据库同步问题: UUID vs 自增主键 - V2EX PostgreSQL 通过分 Database 做多租户可行吗? PostgreSQL 多租户的正确姿势 - V2EX Postgresql 放 K8S 里,大家目前都用的什么方案呢? - V2EX 2025 年 postgresql 有什么优秀的备份解决方案 - V2EX 有大神研究过 pg_resetwal 的原理吗,为什么会丢 user 表的数据 - V2EX 按照 stackoverflow 教程执行 pg_resetwal, postgresql 的数据库被清空了 - V2EX 求国内高性价比的 postgresql 服务,纳米项目 - V2EX 请教一个关于 PostgreSQL 连接被 Server 关闭的问题 - V2EX PostgreSQL 18 JSONB 增加能取代 MongoDB 吗? - V2EX PostgreSql Oracle 迁移 postgreSQL ,有什么经验分享 - V2EX 有什么权限管理最佳实践吗 - V2EX 请教 PostgreSQL 和 PostGIS 使用问题 - V2EX pg 的毫秒级时间戳转换 - V2EX 关于 PostgreSQL 中的 max_connections 和 pg_stat_activity 的疑惑 - V2EX 目前国内最高性价比的 postgresql 服务是? - V2EX 新手,请问 postgres 怎么配置 vacuum - V2EX 学习 PostgreSQL,有什么资料推荐? - V2EX Apache AGE 和 Neo4j 哪个更好? - V2EX 有没有像 Citus 那样方便分片的图数据库? - V2EX 怎样使用 C 语言访问 PostgreSQL 集群? 小系统 数据库 mysql 和 postgresql 比对 和 wordpress 数据库技术选型 PostgreSQL 没有 on update current_timestamp 感觉好麻烦 - V2EX 主从同步使用的端口号可以和数据库端口号不同吗? - V2EX 为什么有时 PostgreSQL 有时会表现得比较卡? - V2EX 第一次使用 PostgreSQL,连建表都不会,请教点经验 Java 有没有针对 PostgreSQL 好用一点的 ORM - V2EX 请教,有无简单易用的 PostgreSQL 表结构、数据的同步工具? - V2EX PostgreSQL 启动失败 - V2EX PostgreSQL 数据库 正常情况下指定了 locale=C 与 encoding=UTF8 (最简单) - V2EX 错误迁移 pgsql 导致日志报 warning,请问如何解决? - V2EX 从 Mongodb 到 PostgreSQL 的大迁移 有无 pg 的入门精进博客或者视频推荐 有什么不通过写脚本的方式,将 postgreSQL 的建表语句转换为 doris 的建表语句吗? - V2EX 新的 PostgreSQL 语言插件 PL/PRQL - V2EX 洗数据:如何将 Contact 和 ContactTag 多对多关联起来 Postgres 在中国还能起来么? - V2EX PostgreSQL 17 增加增量备份的功能 - V2EX
请教一个经典的 postgres 的 sql 怎么写 - V2EX
qweruiop · 2024-02-06 · via PostgreSQL

这是一个创建于 859 天前的主题,其中的信息可能已经有所发展或是发生改变。

现在有 2 张表:student 和 transactions

student 长这样:

id name
1 a
2 b
3 c

transactions

id student_id updated_at
1 1 2024-02-06T16:41:05+08:00
2 1 2024-02-06T13:41:05+08:00
3 2 2024-02-06T12:41:05+08:00

现在想在 postgres 里面用一个 sql ,查出每个用户的 transaction 数量,并且把没有的多一行为 0 。

SELECT s.id AS student_id, s.name, COUNT(t.id) AS transactions_count
FROM student s
LEFT JOIN transactions t ON s.id = t.student_id
GROUP BY s.id, s.name;

但是这样,查出来的结果是

student_id name transactions_count
1 a 2
2 b 1

但是我们需要查出的结果是

student_id name transactions_count
1 a 2
2 b 1
3 c 0

求教各位高手指点下这个 sql 怎么写。。。

28Sv0ngQfIE7Yloe

1

28Sv0ngQfIE7Yloe      2024 年 2 月 6 日   ❤️ 1

奇怪,首先我没用过 pgsql ,但是根据我用 mysql 的经验 ,我有一个疑问,使用 student 作为左表去 left join 右表,为什么会没有 id =3 的数据呢?

Kerwin24

2

Kerwin24      2024 年 2 月 6 日

on 条件不是写错了吗

dongsuo

4

dongsuo      2024 年 2 月 6 日

SELECT s.id AS student_id, s.name, COALESCE (COUNT(t.id),0) AS transactions_count
FROM student s
LEFT JOIN transactions t ON s.id = t.student_id
GROUP BY s.id, s.name;
网上查到的

ss098

5

ss098      2024 年 2 月 6 日

with student_transactions as (
select
transactions.student_id,
count(transactions.id) as transactions_count
from transactions
group by transactions.student_id
)

select
students.id,
students.name,
student_transactions.transactions_count
from students
left join student_transactions on students.id = student_transactions.student_id

Fa11ingWood

6

Fa11ingWood      2024 年 2 月 6 日

SELECT s.id AS student_id, s.name, coalse(COUNT(t.id),0) AS transactions_count
FROM t_ s
LEFT JOIN transactions t ON s.id = t.student_id
GROUP BY s.id, s.name; 试试这样呢

ss098

9

ss098      2024 年 2 月 6 日

刚刚的代码忘记设为 null 了,现在改了一下可以符合预期

with student_transactions as (
select
transactions.student_id,
count(transactions.id) as transactions_count
from transactions
group by transactions.student_id
)

select
students.id,
students.name,
coalesce(student_transactions.transactions_count, 0) as transactions_count
from students
left join student_transactions on students.id = student_transactions.student_id

cookii

10

cookii      2024 年 2 月 7 日 via Android

count ( s.id )就行了

qweruiop

11

qweruiop      2024 年 2 月 7 日

发现一个问题。。。
如果需要列出 2024-02-06T13:41:05+08:00 之后每个学生的交易数量,加上了 where 就不能列出 count 为 0 的行了。。。

```
SELECT s.id AS student_id, s.name, COALESCE(COUNT(t.id),0) AS transactions_count
FROM student s
LEFT JOIN transactions t ON s.id = t.student_id
WHERE t.updated_at>='2024-13-40 00:00:00'
GROUP BY s.id, s.name;
ORDER BY transactions_count DESC
```

出来就只有 1 行了,不加 where ,确实有 3 行。。。再请教下各位高手。。。

| student_id | name | transactions_count |
|------------|------|--------------------|
| 1 | a | 2 |

ss098

13

ss098      2024 年 2 月 7 日

@qweruiop 你可以参照我的方案在 student_transactions 中加上 where 条件,可以解决你的需求。