【问题标题】:Get Count 0 if there are no entries in the RIGHT Table如果 RIGHT 表中没有条目,则获取计数 0
【发布时间】:2023-01-17 01:23:44
【问题描述】:

我有以下 2 个表

Websites
website_Id  | website_name
1           | website_a| 
2           | website_b| 
3           | website_c| 
4           | website_d| 
5           | website_e| 

Fixtures
fixture_Id  | website_id | fixture_details
1           | 1          | a vs b 
2           | 1          | c vs d
3           | 2          | e vs f
4           | 2          | g vs h
5           | 4          | i vs j

现在我想得到一份类似的报告:-

website_Id  | website_name | TotalRows
    1       | website_a    | 2
    2       | website_b    | 2
    3       | website_c    | 0 
    4       | website_d    | 1
    5       | website_e    | 0

如果夹具表中没有条目,我想得到 0。

目前我有以下 SQL:-

Select fx.website_id, ws.website_name, Count (*) as TotalRows 
FROM fixtures fx
LEFT JOIN websites ws on ws.website_id = fx.website_id
WHERE date_of_entry = '16-01-2023'
GROUP BY
  fx.website_id, ws.website_name
;

但是当没有条目时,这不会返回 0 值。

我怎样才能改变我的 SQL 来反映这一点?

预先感谢您的帮助和时间

【问题讨论】:

  • date_of_entry 列在哪个表中?如果它在您的网站表中,那么此 where 子句将过滤掉在 websites 表中没有记录的所有记录。为避免该问题,请将 where 替换为 and,以便此条件成为您的 join 声明的一部分;因此它不会影响 fixtures 表的结果。旁注:您可能需要考虑在 select 和 group by 子句中将 ws.website_name 替换为 coalesce(ws.website_name,'[not found]'),这样您的输出中就没有 nulls 了。
  • date_of_entry 在 fixtures 表中

标签: sql postgresql


【解决方案1】:

试试下面的语句:

SELECT ws.website_id, ws.website_name, COUNT(fx.id) AS number_of_fixtures
FROM websites ws
LEFT JOIN fixtures fx ON fx.website_id = ws.website_id
WHERE TRUE -- or whatever condition you want but I do not know where to take date_of_entry from
GROUP BY ws.website_id

COUNT 将一个表达式作为参数计算该表达式的每一行,如果计算结果为 NULL,则不计入该行。

如果你想坚持你的加入顺序,你需要fixtures RIGHT JOIN websites。

【讨论】:

  • 我尝试了您的查询,它返回的结果与我返回的结果相同,即当 fixtures 表中不存在 website_id 时没有零。 Date_of_entry 是另一个我没有列出的字段,但基本上是一个日期,例如 16-01-2023
【解决方案2】:

问题是你在数*;即不管表的行数;因此,当您返回 1 行时,fixtures 表中只有一条记录,您将得到 1。您可以通过使用 count(ws.website_id) 计算 websites 表中的行来解决这个问题;因为在这个表中有结果的地方,这个字段将返回一个非空值,因此被计算在内;而在没有记录的地方,该字段将为空,因此不计算在内。

Select fx.website_id, ws.website_name, Count (ws.website_id) as TotalRows 
FROM fixtures fx
LEFT JOIN websites ws on ws.website_id = fx.website_id
WHERE fx.date_of_entry = '16-01-2023'
GROUP BY
  fx.website_id, ws.website_name
;

【讨论】:

    【解决方案3】:

    您非常接近,您无法获得计数为 0 的那些记录的原因是因为如果特定网站没有相关的夹具记录,则 date_of_entry 将为 NULL,WHERE date_of_entry = '16-01-2023' 将过滤掉所有这些记录。所以解决方案要么将它放在 LEFT JOIN 条件中,要么在 where 子句中添加一个额外的条件。另一个核心问题是您按网站相关数据对计数进行分组,您必须从网站或 RIGHT JOIN 中选择固定装置以保留所有网站记录显示在结果中。

    溶液A

    Select ws.id AS website_id, ws.website_name, Count (*) as TotalRows 
    FROM websites ws 
    LEFT JOIN fixtures fx on ws.website_id = fx.website_id AND date_of_entry = '16-01-2023'
    GROUP BY
      ws.id, ws.website_name
    ;
    

    溶液B

    Select ws.id AS website_id, ws.website_name, Count (*) as TotalRows 
    FROM websites ws 
    LEFT JOIN fixtures fx on ws.website_id = fx.website_id
    WHERE date_of_entry IS NULL OR date_of_entry = '16-01-2023'
    GROUP BY
      ws.id, ws.website_name
    ;
    

    【讨论】:

    • 嗨,汤姆,解决方案 A 返回所有记录,而不仅仅是 date_of_entry,解决方案 B 返回与我返回的完全相同的记录集,没有零
    • @JMon 请尝试最新更新。我修复了 SQL
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多