The MySQL ifnull function uses the detailed explanation

Source: Internet
Author: User

Usage instructions

1 ifnull (EXPR1,EXPR2)

If EXPR1 is not Null,ifnull () returns EXPR1, it returns EXPR2.

Ifnull () returns a numeric or string value, depending on the context in which it is used.

For example:

The code is as follows Copy Code

Select Ifnull (col1, ' Default-value '), col2 from test;

When the col1 field of the test table is NULL, the result returned by the database is Default-value, otherwise the value of itself is returned. However, when the value of the Col1 field is an empty string ("), the result returned is an empty string because the null character is not NULL. If you need to replace the empty string or null value with Default-value, obviously ifnull is not possible, but use the case time statement can be done, the example is as follows:

The code is as follows Copy Code

Select C1,
(Case when c2 = ' ' or C2 are null then ' default-value ' else C2 end)
From Test;


Let's give some examples:

The code is as follows Copy Code


Mysql> SELECT ifnull (1,0);
+-------------+
| Ifnull (1,0) |
+-------------+
| 1 |
+-------------+
1 row in Set

Since EXPR1 is 1 and is not NULL, the function returns 1. We can try again, if let Expr1 be NULL, whether can return the second parameter? Let Expr1 = 1/0, because the divisor is 0, the result is NULL.

The code is as follows Copy Code

Mysql> SELECT ifnull (1/0, ' nowamagic ');

+-------------------------+
| Ifnull (1/0, ' nowamagic ') |
+-------------------------+
| Nowamagic |
+-------------------------+
1 row in Set

If the previous argument is NULL, the second argument is returned nowamagic.

This function is not difficult to understand, why do you want to introduce this function? This is to tell you the basics of the advanced hacker technology under MySQL, so you can try to understand the first thing

The code is as follows Copy Code

UPDATE table SET views = ' 1 ' WHERE id = -2441 OR (ORD (SELECT ifnull (CAST (FirstName as CHAR), 0x20) from Nowamagic ' ORDER by ID LIMIT 1,1, 2,1) ' >112) #

It uses the Ifnull function, do you understand its function?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.