Instructions for nested use of MySQL ifnull

Instructions for nested use of MySQL ifnull

Nested use of MySQL ifnull

I searched online to see if there is a way to nest ifnull, but it seems that no one has mentioned this usage: most of them simply check whether a field is empty and then replace it with another value;

That is:

select ifnull(a,b) from A ;

But the problem I encountered is that there are two fields, and the scenario is probably like this:

Each store has two discount methods, vip_discount and simple_discount, which are named after the discount field found in the database; vip_discount is the main one.

This means that if vip_discount is not empty, its value will be returned. If its value is empty, it is necessary to further determine whether the simple_discount field is empty. If it is not empty, the value of simple_discount will be returned, otherwise an empty string will be returned.

Here I thought about whether I could use nested ifnull to determine whether I could achieve my goal.

After searching the Internet for a long time and not finding any similar blogs, I thought I could try it myself:

select ifnull(vip_discount,ifnull(simple_discount,"")) as discount from A;

This solves the problem. This SQL statement can also be followed by conditional statements to supplement other conditional queries.

The pitfalls of MYSQL ifnull

Without further ado, let's look at the SQL:

UPDATE
    lb_user u
SET u.user_level = (
    SELECT
        IFNULL(levelid, u.user_level)
    FROM lb_user_level
    WHERE `status` = 1 AND levelid > u.user_level
    AND (upgrade_score < u.empirical OR upgrade_total_money < u.total_consumption_money)
    ORDER BY levelid DESC
    LIMIT 1
);

At first glance, this SQL statement seems to be fine and is executed successfully. However, it fails when executed again. The reason is that the data cannot be retrieved according to the current conditions and the returned data is empty. Note that the data is empty, meaning there is no data, not that the data exists but the field is empty. After investigation, the SQL statement is modified as follows:

UPDATE
    lb_user u
SET u.user_level = IFNULL((
    SELECT
        levelid
    FROM lb_user_level
    WHERE `status` = 1 AND levelid > u.user_level
    AND (upgrade_score < u.empirical OR upgrade_total_money < u.total_consumption_money)
    ORDER BY levelid DESC
    LIMIT 1
), u.user_level);

After the modification, the execution is successful, and there is no IFNULL failure;

To summarize the reasons are:

In MYSQL, IFNULL can only determine if the data exists and is NULL, but cannot determine if the data is not found and is NULL.

The above is my personal experience. 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
  • A brief discussion on ifnull() function similar to nvl() function 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

<<:  Several skills you must know when making web pages

>>:  HTML displays ellipsis beyond the text... implemented through text-overflow

Recommend

Detailed explanation of webpack-dev-server core concepts and cases

webpack-dev-server core concepts Webpack's Co...

Vue resets data to its initial state

In some cases, the data in data needs to be reuse...

LINUX Checks whether the port is occupied

I have never been able to figure out whether the ...

Implementing custom radio and check box functions with pure CSS

1. Achieve the effect 2 Knowledge Points 2.1 <...

Linux 6 steps to change the default remote port number of ssh

The default ssh remote port in Linux is 22. Somet...

Solution to MySQL garbled code problem under Linux

The project interacts with the server, accesses t...

Solution to the conflict between nginx and backend port

question: When developing the Alice management sy...

18 common commands in MySQL command line

In daily website maintenance and management, a lo...

Analysis of the principle and creation method of Mysql temporary table

This article mainly introduces the principle and ...

3 ways to add links to HTML select tags

The first one : Copy code The code is as follows: ...

JavaScript uses promise to handle multiple repeated requests

1. Why write this article? You must have read a l...

Control the vertical center of the text in the HTML text box through CSS

When the height attribute of Text is defined, the ...

How to install MySQL via SSH on a CentOS VPS

Type yum install mysql-server Press Y to continue...

MySQL advanced learning index advantages and disadvantages and rules of use

1. Advantages and Disadvantages of Indexes Advant...

Implementation of dynamic rem for mobile layout

Dynamic rem 1. First, let’s introduce the current...