Search code examples
mysqlifnull

MySQL IFNULL is not returning result


I am trying to fetch remaining quantity of books with following approach

stock = total+(receive-Issued);

Table book_qnt - lists of book Quantiy

id  |   book    |   qnt
=======================
1   |   1       |   20  
2   |   2       |   12  

Table book - lists of Book

id  |   ttl
===========
1   |   Social Experiment
2   |   Evolution of Culture

Table book_trns - lists of book transcation

id  |   bk  |   std |   iss |   rcv |   dte
==========================================
1   |   2   |   1   |   6   |   0   |   2019-04-11
2   |   2   |   2   |   4   |   0   |   2019-04-05

It is fine for only those books which has receive and issued values.

In DB language, it is displaying result of those books which book_trans.rcv and book_trans.iss is not NULL

SELECT 
book.id AS book, 
book_qnt.qnt+((SUM(book_trans.rcv))-(SUM(book_trans.iss))) AS stock,

Result

book|   stock
===========
1   |   NULL    
2   |   2

Now I am try to fetch result as following, if book_trans.rcv and book_trans.iss is NULL, then it should fetch total qnt from book_qnt

Desired Result

book|   stock
===========
1   |   20   // id 1's book_trans.rcv and book_trans.iss is NULL, so it should show total qnt
2   |   2    // id 2's book_trans.rcv and book_trans.iss is not NULL, so it is calculating

What I do if book_trans.rcv and book_trans.iss row is Null, then assign value as 0.

SELECT 
    book.id AS book, 
    book_qnt.qnt+((IFNULL(0, SUM(book_trans.rcv)))-(IFNULL(0, SUM(book_trans.iss)))) AS stock,

But Result (calculation doesn't work)

book|   qnt
===========
1   |   20
2   |   12

Full MySQL

SELECT 
    book_qnt.qnt+((IFNULL(0, SUM(book_trans.rcv)))-(IFNULL(0, SUM(book_trans.iss)))) AS stock,
    lib_bk.id, 
    lib_bk.ttl 
FROM 
    book
JOIN 
    book_qnt ON book_qnt.book=book.id 
LEFT JOIN
    book_trans ON book_trans.bk=book.id
GROUP BY book.id

Solution

  • Your 0 replacement in the null test is the wrong way round. Try this

    SELECT 
         book_qnt.qnt , SUM(book_trns.rcv), SUM(book_trns.iss),
         ((IFNULL(SUM(book_trns.rcv),0))-(IFNULL(SUM(book_trns.iss),0))),
         book_qnt.qnt+((IFNULL(SUM(book_trns.rcv),0))-(IFNULL(SUM(book_trns.iss),0))) AS stock,
        book.id, 
        book.ttl 
    FROM 
        book
    JOIN 
        book_qnt ON book_qnt.book=book.id 
    LEFT JOIN
        book_trns ON book_trns.bk=book.id
    GROUP BY book.id;
    
    +------+--------------------+--------------------+-----------------------------------------------------------------+-------+------+----------------------+
    | qnt  | SUM(book_trns.rcv) | SUM(book_trns.iss) | ((IFNULL(SUM(book_trns.rcv),0))-(IFNULL(SUM(book_trns.iss),0))) | stock | id   | ttl                  |
    +------+--------------------+--------------------+-----------------------------------------------------------------+-------+------+----------------------+
    |   20 |               NULL |               NULL |                                                               0 |    20 |    1 | Social Experiment    |
    |   12 |                  0 |                 10 |                                                             -10 |     2 |    2 | Evolution of Culture |
    +------+--------------------+--------------------+-----------------------------------------------------------------+-------+------+----------------------+
    2 rows in set (0.00 sec)
    

    And do try to keep your table names consistent with the query.