如何粘合两个相关表?
How to glue two dependent tables?
我有 Customer 和 Application 表。我想创建 select 查询来提供有关客户的信息,并计算用户在系统中拥有的应用程序数量。
select distinct c.id, c.region, c.city, count(a.customer_id_id)
from customers c
join applications a on c.id=a.customer_id_id
group by c.id;
但是我得到一个错误,我需要按地区和城市分组,但我想显示有关每个应用程序的信息,而不是按地区和城市分组。因为通过这种方式,我不会为每个用户获得大量应用程序,而是为每个用户组获得。
我读到可以使用嵌套查询和完全外部连接,但我试过了但没有成功。你能告诉我怎么做吗?
你很接近。
使用 LEFT OUTER JOIN
以便 Customers
在 Applications
中有 0 条记录也将包括在内(假设您的意图在这里)
不要同时使用 DISTINCT
和 GROUP BY
。不同意味着 "If all the fields are the same value across multiple records in the record set produced by this SELECT statement, then only give back distinct records, dropping the duplicates"。取而代之的是 GROUP BY
、"Group by this list of fields. Any remaining fields not in this list will be aggregated using a formula in your SELECT clause like count(a.customer_id_id)
." 它们是相似的,但是你不能只用一个 DISTINCT 聚合一个字段。
当使用 GROUP BY
时,如果您不打算使用聚合公式聚合字段 (count
, sum
, avg
,等..)那么你必须将它包含在你的组中。对于某些 RDBMS(例如 MySQL 的旧版本),这不是必需的,但这是一种糟糕的做法,因为未明确与 GROUP BY
中也缺少的公式聚合的字段就像告诉 RDBMS "Just pick which ever value you wish from matching records" 这可能会产生一些意想不到的后果。
SELECT c.id, c.region, c.city, count(a.customer_id_id)
FROM customers c
LEFT OUTER JOIN applications a on c.id=a.customer_id_id
GROUP BY c.id, c.region, c.city;
不确定你的问题是什么。我假设 region 和 city 在功能上依赖于 id (即 id 是候选键)。因此,较新版本的 postgresql 将接受您的查询。但是,如果您使用的是旧版本,则可以将 group by 子句扩展为:
select c.id, c.region, c.city, count(a.customer_id_id)
from customers c
join applications a
on c.id=a.customer_id_id
group by c.id, c.region, c.city;
你说你想显示每个应用程序的信息,但为什么要计算每个客户的应用程序数量?你的意思是:
select c.id, c.region, c.city, a.customer_id_id, a.<other attributes>
from customers c
join applications a
on c.id=a.customer_id_id;
我有 Customer 和 Application 表。我想创建 select 查询来提供有关客户的信息,并计算用户在系统中拥有的应用程序数量。
select distinct c.id, c.region, c.city, count(a.customer_id_id)
from customers c
join applications a on c.id=a.customer_id_id
group by c.id;
但是我得到一个错误,我需要按地区和城市分组,但我想显示有关每个应用程序的信息,而不是按地区和城市分组。因为通过这种方式,我不会为每个用户获得大量应用程序,而是为每个用户组获得。 我读到可以使用嵌套查询和完全外部连接,但我试过了但没有成功。你能告诉我怎么做吗?
你很接近。
使用
LEFT OUTER JOIN
以便Customers
在Applications
中有 0 条记录也将包括在内(假设您的意图在这里)不要同时使用
DISTINCT
和GROUP BY
。不同意味着 "If all the fields are the same value across multiple records in the record set produced by this SELECT statement, then only give back distinct records, dropping the duplicates"。取而代之的是GROUP BY
、"Group by this list of fields. Any remaining fields not in this list will be aggregated using a formula in your SELECT clause likecount(a.customer_id_id)
." 它们是相似的,但是你不能只用一个 DISTINCT 聚合一个字段。当使用
GROUP BY
时,如果您不打算使用聚合公式聚合字段 (count
,sum
,avg
,等..)那么你必须将它包含在你的组中。对于某些 RDBMS(例如 MySQL 的旧版本),这不是必需的,但这是一种糟糕的做法,因为未明确与GROUP BY
中也缺少的公式聚合的字段就像告诉 RDBMS "Just pick which ever value you wish from matching records" 这可能会产生一些意想不到的后果。
SELECT c.id, c.region, c.city, count(a.customer_id_id)
FROM customers c
LEFT OUTER JOIN applications a on c.id=a.customer_id_id
GROUP BY c.id, c.region, c.city;
不确定你的问题是什么。我假设 region 和 city 在功能上依赖于 id (即 id 是候选键)。因此,较新版本的 postgresql 将接受您的查询。但是,如果您使用的是旧版本,则可以将 group by 子句扩展为:
select c.id, c.region, c.city, count(a.customer_id_id)
from customers c
join applications a
on c.id=a.customer_id_id
group by c.id, c.region, c.city;
你说你想显示每个应用程序的信息,但为什么要计算每个客户的应用程序数量?你的意思是:
select c.id, c.region, c.city, a.customer_id_id, a.<other attributes>
from customers c
join applications a
on c.id=a.customer_id_id;