mysql using sum () null problems arise, and various summaries

When sql query today, ran into a problem:
SQL statement:
the SELECT r.user_id, r.job_id, SUM (t.money) AS job_salary
from pj_punch_records left the Join pj_punch_transactions AS AS r ON r.job_id t = t.transaction_detail
the WHERE r = = 1 and t.type .user_id 1
the Order by r.created_at desc

1.进行2表关联查询,同时,使用 sum() 来统计金钱总数,主表,并没有对应查询条件的记录,居然出现了一条全部为 NULL 的记录。这个不合理啊!我想要的自然是,没有查询到合适的数据,结果集应该为空!
2.上面这种情况,当有记录时,就正常了!

Solution:
is the next random testing, found after adding 'group by r.job_id', found that the result set is empty!
Not studied mysql, had no time to take a look at mysql knowledge areas. However, the above two cases, guess:
SELECT - mysql as the output method, similar to the echo like php
encountered sum () avg () built-in function mysql like, or other non-direct current fields, query data table (either other statements, either in the field of the data table what has been done deal!), will output a row of data!
Therefore, only one line we see all NULL data!
Group by r.job_id using the results of the polymerization, the record to NULL filtered so as to return the 'empty result set'

When searching online, also happens to find a deal with this problem, methods used to: Group by NULL
http://www.xuebuyuan.com/2008066.html

More online relates sum () return null, is to return to 0, just under where also notes:
1. The first: When using IFNULL (expr1, expr2) function, when expr1 is NULL, then return the default value data expre2
the SELECT IFNULL (SUM (expr), 0 ) - said that if the SUM () function returns the result returns NULL Chak 0
2. the second: using the COALESCE (value, ...) function, COALESCE function returns the passed parameter is the role of a first non-null value in
the SELECT the COALESCE (the SUM (expr), 0)
3. third: the case WHEN THEN WHEN THEN .. ELSE eND function, attention CASE WHEN function is the last end of the eND
the SELECT CASE WHEN the ISNULL ( SUM (expr)) THEN 0 ELSE SUM (expr) END

参考文章:
    http://blog.csdn.net/h70614959/article/details/40115395
    http://blog.csdn.net/qq844579582/article/details/52610484

可查看 SQL NULL 函数:
    ISNULL()、NVL()、IFNULL()、COALESCE(),这几个函数可能效果差不多
参考:
    http://www.w3school.com.cn/sql/sql_isnull.asp

SQL, AVG, COUNT, SUM, MAX NULL value processing function like
a, the AVG ()
averaging
Note AVE () ignore NULL values, rather than as "0" involved in the calculation
two, COUNT (), two kinds of usage
1, cOUNT (*)
to count the number of rows in the table
regardless of whether there is NULL

        2、COUNT(字段名)
        对特定列有数据的行进行计数
        忽略NULL值

Three, MAX (), MIN ()
seeking the maximum, minimum values
are ignored NULL
four, the SUM ()
can be a single column summation, may be summed after the plurality of column computation
ignored NULL value, and when the operation of a plurality of columns when summed, if operation of any one of the column is NULL, the record line is ignored.
For example: SUM (A + B + C ), A, B, C is three, if a row in column A record is NULL, no statistical line.
Five, GROUP BY Note the use of
1, if the column NULL packets, which will also serve as a set, and the NULL value is at the top
2, in addition to the summary statement function calculation, the selected column must be present in the SELECT GROUP BY in
3, GROUP BY can contain any number of columns, can be nested
reprint articles:
http://m.studyofnet.com/news/1219.html

Guess you like

Origin www.cnblogs.com/ljt1412451704/p/12074612.html