Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the conditional judgment functions in MySQL

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about the conditional judgment functions in MySQL, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Three conditional judgment functions:

[1] IF ()

First take a look at its syntax format: IF (expr,v1,v2)

Take a look at its execution logic: if expr is TRUE (that is, expr0andexprNULL), the function returns v1, otherwise v2.

Therefore, we conclude that the value returned is specified in the input parameters of the function.

Then let's start with a simple case and explore its real value from shallow to deep.

When IF (2019 > 2018quot true lies) executes the error report, we can know that the value that can be specified in the function is reserved for MySQL.

From the return result of 1 instead of TRUE from IF (2019 > 2018 TRUE trust false), we can see that MySQL has done another operation on TRUE, that is, TRUE is 1.

Then we try to see if we can encapsulate other complex logic (such as functions) in expr.

The function STRCMP ('AEV','UCR') evaluates to-1, which is neither equal to 0 nor NULL

Based on the return value of the function STRCMP ('AEV','UCR'), the result returned by IF () is v1 (that is, YES).

[2] IFNULL ()

The function is relatively simple, from its syntax format IFNULL (v1PowerV2), we can see that it only needs to define the return value and does not need to define the judgment expression, because the function name itself is the meaning of judgment.

Determine whether the two parameters are NULL

If neither parameter is empty, the first parameter value is returned

If the first is empty, the second parameter value is returned

Two parameters can also encapsulate more complex operations (such as functions).

[3] CASE

If we look at the grammatical format, we will find that it is troublesome to write this kind of function, but the logic of its expression is closest to our daily habits of thinking.

[WHENvnTHENrn]... [ELSEr (nasty 1)] END or CASEWHENv1THENr1 [where v2THENr2]... [WHENvnTHENrn]... ELSEr (nasty 1)] END

To read this judgment function in popular words is to return the value R1 if condition expr satisfies condition v1 and R2 if condition v2 is satisfied. Otherwise, a specified value r (nail1) is returned.

[conclusion] the effects of the two writing methods are the same.

After reading the above, do you have any further understanding of the conditional judgment functions in MySQL? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report