Knowledge Walls
John Peter
Pune, Maharashtra, India
IFNULL method with Example in Cool Topics of MySQL
5572 Views
IFNULL method is one of decision making method of mysql. It is used to avoid repeating of statement while using IF. When checking output value has null then send empty otherwise output value.

Syntax:
    IFNULL(exp1,exp2);

    If exp1 is not null returns exp1 value otherwise exp2.
How it works 
Disadvantage:
   Using IF() method
   IF (group_concat(role_id) IS NOT NULL,group_concat(role_id),"")

   When using IF() with NULL value check group_concat() method is executing two times.

Advantage:
   Using IFNULL() method
   IFNULL(group_concat(role_id),"")

   When using IFNULL automatically return group_concat() output when it is not null otherwise returns empty string. So group_concat method is executing one time only when using IFNULL.
Example
SELECT IFNULL(group_concat(role_id),"") as role_id_list FROM user_info WHERE user_id = 10;
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details