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

MySQL explains the example of conditional judgment function.

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article mainly introduces MySQL to explain the conditional judgment function example, has a certain reference value, interested friends can refer to, I hope you read this article after a lot of gains, the following let Xiaobian take you to understand.

Function:

IF(expr,v1,v2) function

(2)IFNULL(v1,v2) function

â-o January (3)

(Related free learning recommendation: mysql video tutorial)

(1)if(expr,v1,v2) function

In the if(expr,v1,v2) function, return v1 if expr is true(expr0 and eprnull), otherwise return v2.

[Example] Use the if() function to make conditional judgments. The SQL statement is as follows:

mysql> select if(1>2,2,3), -> if(1 if(strcmp('test','test1'),'no','yes');+-------------+--------------------+---------------------------------------+| if(1>2,2,3) | if(1 select ifnull(1,2),ifnull(null,10),ifnull(1/0,'wrong');+-------------+-----------------+---------------------+| ifnull(1,2) | ifnull(null,10) | ifnull(1/0,'wrong') |+-------------+-----------------+---------------------+| 1 | 10 | wrong |+------------------------------------------+1 row in set (0.00 sec)(3)case function

1.case expr when v1 then r1 [ when v2 then r2] [else rn] end

This function says that if expr is equal to vi, it returns the result after then, and if expr is not equal to all values, it returns m after else.

[Example] Use case value when statement to execute branch operation. SQL statement is as follows:

mysql> select case 2 when 1 then 'one' when 2 then 'two' else 'more' end;+------------------------------------------------------------+| case 2 when 1 then 'one' when 2 then 'two' else 'more' end |+------------------------------------------------------------+| two |+------------------------------------------------------------+1 row in set (0.00 sec)

2.case when v1 then rv [when v2 then r2] else rn] end

This function indicates that if a vi value is true, it returns the result after the corresponding position then, and if all values are not true, it returns rn after else.

The default return value type of a case expression is any compatible set type of return values, depending on the context.

[Example] Use case when statement to execute branch operation. SQL statement is as follows:

mysql> select case when 1

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