Sunday, August 19, 2012

MySQL - isnull usage


SELECT book.code, book.bookname, book.author, book.publisher, ifnull(cc.testtime,0)
------------------------------------------------------------------------------------------------------
SELECT book.code, book.bookname, book.author, book.publisher, if(cc.testtime is null,0,cc.testtime)
------------------------------------------------------------------------------------------------------
SELECT book.code, book.bookname, book.author, book.publisher, if(isnull(cc.testtime),0,cc.testtime)
------------------------------------------------------------------------------------------------------

 as borrow_times
FROM book LEFT JOIN (select loan.code as testcode ,count(loan.stuid) as testtime
from loan
group by loan.code)  AS cc ON book.code=cc.testcode
ORDER BY book.code;


===============================
Result:
===============================
codebooknameauthorpublisherborrow_times
c001HarryCharliecharlie2
c002The RingAndyTonli1
c003KonanDowaTonli0

No comments:

Post a Comment