简体   繁体   English

如何将请求的 SELECT 中的 PostgreSQL 数据透视表字段 (foo_id) 替换为其从 foo 表中获取的值?

[英]How to replace a PostgreSQL pivot table field (foo_id) in the SELECT of a request by its value taken from the foo table?

Below is my current request made in the foo_bar pivot table:以下是我在foo_bar数据透视表中提出的当前请求:

SELECT
    foo_id AS foo,
    COUNT(foo_id) AS total
FROM foo_bar
GROUP BY foo_id

How to replace the value of foo_id in the SELECT by foo.name taken from foo table ?如何更换的价值foo_idSELECT通过foo.name取自foo表?

You need to join the table foo , assuming both have a foo_id to join the tables:您需要加入表foo ,假设两者都有一个foo_id来加入表:

SELECT
    f.name,
    COUNT(fb.foo_id) AS total
FROM foo_bar fb
JOIN foo f
   ON f.foo_id = fb.foo_id
GROUP BY f.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM