简体   繁体   English

获取一个SQL查询以打印0表示3个表中的空计数结果

[英]Getting a SQL query to print 0 for null count results across 3 tables

I'm trying to get a SQL query to give me the results of a count but I need the result to include rows where the count is 0. What I found for solutions to this was to use IFNULL(COUNT(*), 0) in place of COUNT(*) however that had no effect on the result. 我试图获取一个SQL查询以提供计数结果,但我需要该结果包含计数为0的行。为此找到的解决方案是使用IFNULL(COUNT(*), 0)代替COUNT(*)但是对结果没有影响。 I also tried using a LEFT JOIN but SQL gave me a syntax error if I tried to put in those. 我也尝试过使用LEFT JOIN但是如果我尝试添加那些,SQL会给我一个语法错误。 Here's my table setup 这是我的桌子设置

User 用户

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| UserID      | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| firstName   | varchar(15)  | NO   |     | NULL    |                |
| lastName    | varchar(15)  | NO   |     | NULL    |                |
| Protocol    | varchar(10)  | NO   |     | NULL    |                |
| Endpoint    | varchar(50)  | NO   |     | NULL    |                |
| UsergroupID | mediumint(9) | NO   | MUL | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

Subscription 订阅

+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| SubscriptionID | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| TopicID        | mediumint(9) | NO   | MUL | NULL    |                |
| UserID         | mediumint(9) | NO   | MUL | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+

Topic 话题

+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| TopicID  | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| Name     | varchar(50)  | NO   |     | NULL    |                |
| FBName   | varchar(30)  | YES  |     | NULL    |                |
| FBToken  | varchar(255) | YES  |     | NULL    |                |
| TWName   | varchar(10)  | YES  |     | NULL    |                |
| TWToken  | varchar(50)  | YES  |     | NULL    |                |
| TWSecret | varchar(50)  | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

My SQL query to try and get the COUNT is : 我的SQL查询尝试获取COUNT是:

SELECT Topic.TopicID as ID, Topic.Name AS TopicName, COUNT(*) AS numSubscriptions
FROM User, Topic, Subscription
WHERE Subscription.UserID = User.UserID
  AND Subscription.TopicID = Topic.TopicID
GROUP BY Topic.TopicID;

I've tried replacing COUNT(*) with IFNULL(COUNT(*), 0) and I've tried to replace User,Topic,Subscription with User JOIN Subscription JOIN Topic and I also tried User LEFT JOIN Subscription LEFT JOIN Topic but that got a SQL error. 我试着更换COUNT(*)IFNULL(COUNT(*), 0)和我一直试图取代User,Topic,SubscriptionUser JOIN Subscription JOIN Topic ,我也试过User LEFT JOIN Subscription LEFT JOIN Topic但出现SQL错误。

The output I'm getting is: 我得到的输出是:

+----+-----------+------------------+
| ID | TopicName | numSubscriptions |
+----+-----------+------------------+
|  2 | test      |                2 |
|  3 | test2     |                1 |
+----+-----------+------------------+

I need to be getting 我需要得到

+----+-----------+------------------+
| ID | TopicName | numSubscriptions |
+----+-----------+------------------+
|  2 | test      |                2 |
|  3 | test2     |                1 |
|  4 | test3     |                0 |
+----+-----------+------------------+

By default, outer joins are left to right. 默认情况下,外部联接从左到右。 So, the trick is to start with Topic: 因此,诀窍是从Topic开始:

SELECT Topic.TopicID as ID, Topic.Name AS TopicName,
  COUNT(User.UserID) AS numSubscriptions
FROM Topic
LEFT JOIN Subscription
  ON Subscription.TopicID = Topic.TopicID
JOIN User
  ON User.UserID = Subscription.UserID
GROUP BY Topic.TopicID

This allows for multiple subscriptions per user and requires that the user record exists to be considered in the count. 这允许每个用户进行多个订阅,并要求在计数中考虑用户记录的存在。

COUNT(NULL) evaluates to 0 , so any topic records without a corresponding subscription and user record will show as 0 . COUNT(NULL)计算结果为0 ,因此任何没有相应订阅和用户记录的主题记录将显示为0

If you're not concerned whether the user record exists, you could simplify it to the following: 如果您不担心用户记录是否存在,可以将其简化为以下内容:

SELECT Topic.TopicID as ID, Topic.Name AS TopicName,
  COUNT(Subscription.TopicID) AS numSubscriptions
FROM Topic
LEFT JOIN Subscription
  ON Subscription.TopicID = Topic.TopicID
GROUP BY Topic.TopicID

The example below should do what you're after. 下面的示例应执行的操作。 The column in the COUNT() can be any column of the subscription table, but using its ID is a good practice. COUNT()中的列可以是订阅表的任何列,但是使用其ID是一种很好的做法。

Using the left join ensures that all entries of the user table will show up in the results, even if there are no matching subscriptions. 使用左联接确保即使没有匹配的预订,用户表的所有条目也会显示在结果中。

SELECT User.firstName,
       User.lastName,
       Topic.Name AS TopicName,
       COUNT(Subscription.SubscriptionId) AS numSubscriptions
FROM USER
LEFT OUTER JOIN Subscription ON Subscription.UserID=USER.UserID
LEFT OUTER JOIN Topic ON Subscription.TopicID=Topic.TopicID
GROUP BY User.firstName, User.lastName, Topic.Name;

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

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