简体   繁体   English

MySQL SUM 0在JOIN上没有结果

[英]MySQL SUM 0 for no result on JOIN

I have two tables, 'authorization' and 'transaction'. 我有两个表,'授权'和'交易'。

authorization table 授权

Auth_ID | User_ID | Auth_Hours
-------------------------------
5       | 1       | 60
6       | 2       | 40
7       | 3       | 50

transaction table 交易

Auth_ID | User_ID | Used_Hours
-------------------------------
5       | 1       | 5
6       | 2       | 2
5       | 1       | 20
6       | 2       | 17
5       | 1       | 11
6       | 2       | 9

I want my query to sum Used_Hours and group by Auth_ID and User_ID from the transaction table. 我希望我的查询总结Used_Hours并通过事务表中的Auth_IDUser_ID进行分组。 Here's the catch - I also want the query result to show users who have not used any of their Auth_Hours with a 0. See below: 这是捕获 - 我还希望查询结果显示未使用任何Auth_Hours的用户为0.见下文:

QUERY QUERY

SELECT a.Auth_ID, a.USER_ID, a.Auth_Hours, SUM(t.Used_Hours)
FROM AUTHORIZATION a
JOIN TRANSACTION t
on a.Auth_ID = t.Auth_ID
and a.User_ID = t.User_ID
GROUP BY a.Auth_ID, a.USER_ID, a.Auth_Hours

Actual Result 实际结果

Auth_ID | User_ID | Auth_Hours | Total Hours Used
-------------------------------------------------
5       | 1       | 60         | 36
6       | 2       | 40         | 28

Wanted Result 想要的结果

Auth_ID | User_ID | Auth_Hours | Total Hours Used
-------------------------------------------------
5       | 1       | 60         | 36
6       | 2       | 40         | 28
7       | 3       | 50         | 0

I would imagine the query to be relatively simple. 我认为查询相对简单。

The JOIN statement is a shortcut for INNER JOIN which returns records that have matching values in both tables. JOIN语句是INNER JOIN的快捷方式,它返回两个表中具有匹配值的记录。 If you want to return all records from one table and the matching records from the other table, then you should use an outer join ( LEFT [OUTER] JOIN or RIGHT [OUTER] JOIN ). 如果要返回一个表中的所有记录以及另一个表中的匹配记录,则应使用外连接( LEFT [OUTER] JOINRIGHT [OUTER] JOIN )。 Then you can use the IFNULL() or COALESCE() functions to convert NULL s to zeros: 然后你可以使用IFNULL()COALESCE()函数将NULL转换为零:

SELECT a.Auth_ID, a.USER_ID, a.Auth_Hours, IFNULL(SUM(t.Used_Hours), 0) AS 'Total Hours Used'
FROM authorization a
LEFT JOIN transaction t ON a.Auth_ID = t.Auth_ID AND a.User_ID = t.User_ID
GROUP BY a.Auth_ID, a.USER_ID, a.Auth_Hours

Notice that I used single quotes to assign a string with spaces as a name to the total field (as you used in your examples). 请注意,我使用单引号将带空格的字符串作为总字段的名称(如示例中所使用的那样)。 This will work in all databases. 这适用于所有数据库。 In MySQL you can also use back ticks, but that only works in MySQL. 在MySQL中你也可以使用反向标记,但这只适用于MySQL。

Here is a good illustration about the different types of joining tables . 以下是关于不同类型的连接表很好的说明

this query work in any mysql Engine or Version. 此查询适用于任何mysql引擎或版本。 use this query: 使用此查询:

SELECT a.Auth_ID, a.USER_ID, a.Auth_Hours, CASE COUNT(t.Used_Hours) WHEN 0 THEN 0 ELSE SUM(t.Used_Hours) END AS 'Total Hours Used'
FROM authorization a
LEFT JOIN TRANSACTION t ON a.Auth_ID = t.Auth_ID AND a.User_ID = t.User_ID
GROUP BY a.Auth_ID, a.USER_ID, a.Auth_Hours

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

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