A brief discussion on ifnull() function similar to nvl() function in MySQL

A brief discussion on ifnull() function similar to nvl() function in MySQL

IFNULL(expr1,expr2)

If expr1 is not NULL, IFNULL() returns expr1, otherwise it returns expr2. IFNULL() returns a numeric or string value, depending on the context in which it is used.

mysql> select IFNULL(1,0);
    -> 1
mysql> select IFNULL(0,10);
    -> 0
mysql> select IFNULL(1/0,10);
    -> 10
mysql> select IFNULL(1/0,'yes');
    -> 'yes'
 
IF(expr1,expr2,expr3)

If expr1 is TRUE (expr1<>0 and expr1<>NULL), then IF() returns expr2, otherwise it returns expr3. IF() returns a number or string value, depending on the context in which it is used.

mysql> select IF(1>2,2,3);
    -> 3
mysql> select IF(1<2,'yes','no');
    -> 'yes'
mysql> select IF(strcmp('test','test1'),'yes','no');
    -> 'no'

expr1 is evaluated as an integer value, which means that if you are testing a floating point or string value, you should use a comparison operator to do so.

mysql> select IF(0.1,1,0);
    -> 0
mysql> select IF(0.1<>0,1,0);
    -> 1

In the first case above, IF(0.1) returns 0 because 0.1 is converted to an integer value, resulting in the test IF(0). This might not be what you expected. In the second case, the comparison tests the original floating-point value to see if it is nonzero, and the result of the comparison is used as an integer.

CASE value WHEN [compare-value] THEN result [WHEN [compare-value] THEN result ...] [ELSE result] END 
  
CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END 

The first version returns result where value = compare-value. The second version returns result if the first condition is true. If there is no matching result value, the result after ELSE is returned. If there is no ELSE part, then NULL is returned.

mysql> SELECT CASE 1 WHEN 1 THEN "one" WHEN 2 THEN "two" ELSE "more" END;
    -> "one"
mysql> SELECT CASE WHEN 1>0 THEN "true" ELSE "false" END;
    -> "true"
mysql> SELECT CASE BINARY "B" when "a" then 1 when "b" then 2 END;
-> NULL

The above is a brief discussion on the ifnull() function in MySQL which is similar to the nvl() function. This is all I want to share with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Solutions to invalid is Null segment judgment and IFNULL() failure in MySql
  • Solution to MySQL IFNULL judgment problem
  • Summary of knowledge points related to null (IFNULL, COALESCE and NULLIF) in MySQL
  • Detailed explanation of IFNULL() and COALESCE() functions to replace null in MySQL
  • Detailed explanation of IFNULL, NULLIF and ISNULL usage in MySql
  • A brief discussion on the usage of SQL Server's ISNULL function and MySQL's IFNULL function
  • Introduction to the difference between IFNULL, IF, and CASE in MySQL
  • Instructions for nested use of MySQL ifnull

<<:  How to set up PostgreSQL startup on Ubuntu 16.04

>>:  Case study of dynamic data binding of this.$set in Vue

Recommend

Understand the principle of page replacement algorithm through code examples

Page replacement algorithm: The essence is to mak...

Recommend a cool flashing alarm button

The effect is as follows: The code is as follows ...

Detailed explanation of the process of using GPU in Docker

Table of contents Download tf-gpu Build your own ...

HTML+CSS to implement the sample code of the navigation bar drop-down menu

Effect The pictures in the code can be changed by...

How to smoothly upgrade and rollback Nginx version in 1 minute

Today, let's talk about a situation that is o...

MySQL 5.7.21 installation and configuration tutorial under Window10

This article records the installation and configu...

Solution - BASH: /HOME/JAVA/JDK1.8.0_221/BIN/JAVA: Insufficient permissions

1) Enter the folder path where the jdk file is st...

Implementing access control and connection restriction based on Nginx

Preface Nginx 's built-in module supports lim...

Summary of methods to clear cache in Linux system

1) Introduction to cache mechanism In the Linux s...

How to install rabbitmq-server using yum on centos

Socat needs to be installed before installing rab...

jquery+springboot realizes file upload function

This article example shares the specific code of ...

Docker image management common operation code examples

Mirroring is also one of the core components of D...

How to split and merge multiple values ​​in a single field in MySQL

Multiple values ​​combined display Now we have th...

HTML table tag tutorial (20): row background color attribute BGCOLOR

The BGCOLOR attribute can be used to set the back...

How to view files in Docker image

How to view files in a docker image 1. If it is a...