Two examples of if and ifnull in MySQL

Source: Internet
Author: User
Tags strcmp

1.IFNULL statement: Ifnull (EXP1, EXP2), return EXP2 if EXP1 is null, return EXP1 if not null

The code is as follows Copy Code

mysql> SELECT ifnull (null, 100);
+-------------------+
| Ifnull (NULL, 100) |
+-------------------+
| 100 |
+-------------------+

mysql> SELECT ifnull (0, 100);
+----------------+
| Ifnull (0, 100) |
+----------------+
| 0 |
+----------------+

Mysql> SELECT Ifnull (-10, 100);
+------------------+
| Ifnull (-10, 100) |
+------------------+
| -10 |
+------------------+

Mysql> SELECT ifnull (10, 100);
+-----------------+
| Ifnull (10, 100) |
+-----------------+
| 10 |
+-----------------+

mysql> SELECT ifnull (' null ', 100);
+---------------------+
| Ifnull (' null ', 100) |
+---------------------+
| null |
+---------------------+

Mysql> SELECT Ifnull (false, 100);
+--------------------+
| Ifnull (False, 100) |
+--------------------+
| 0 |
+--------------------+

Mysql> SELECT Ifnull (true, 100);
+-------------------+
| Ifnull (True, 100) |
+-------------------+
| 1 |
+-------------------+

2.IF statement: if (EXP1, EXP2, EXP3) If EXP1 is true (exp1 <> 0 && exp1 <> null)


Return to EXP2 or return EXP3

The code is as follows Copy Code

Mysql> SELECT IF (STRCMP (' str ', ' str1 '), ' yes ', ' no ');
+----------------------------------------+
| IF (STRCMP (' str ', ' str1 '), ' yes ', ' no ') |
+----------------------------------------+
| Yes |
+----------------------------------------+

mysql> SELECT IF (0, ' yes ', ' www.111cn.net ');
+--------------------+
| IF (0, ' yes ', ' no ') |
+--------------------+
| No |
+--------------------+

mysql> SELECT IF (null, ' yes ', ' no ');
+-----------------------+
| IF (null, ' yes ', ' no ') |
+-----------------------+
| No |
+-----------------------+

mysql> SELECT IF (' null ', ' yes ', ' no ');
+-------------------------+
| IF (' null ', ' yes ', ' no ') |
+-------------------------+
| No |
+-------------------------+

Mysql> SELECT IF (false, ' yes ', ' no ');
+------------------------+
| IF (False, ' yes ', ' no ') |
+------------------------+
| No |
+------------------------+

Mysql> SELECT IF ( -10, ' yes ', ' no ');
+----------------------+
| IF ( -10, ' yes ', ' no ') |
+----------------------+
| Yes |
+----------------------+

Mysql> SELECT IF (Ten, ' Yes ', ' no ');
+---------------------+
| IF (+, ' yes ', ' no ') |
+---------------------+
| Yes |
+---------------------+

mysql> SELECT IF (' 0 ', ' yes ', ' no ');
+----------------------+
| IF (' 0 ', ' yes ', ' no ') |
+----------------------+
| No |
+----------------------+

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.