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

How to use the if statement in sql

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

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use the if sentence in sql. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Three parameters in the if statement in sql are expr1,expr2,expr3. When the value of expr1 is true, the return value of the function is expr2, when the value of expr1 is false, the return value of the function is expr3

In the project development, the use of database can not be avoided, if the sql database first query the data in the data display processing. Such an operation would be troublesome and inefficient. We can change another way to operate directly in the sql statement, the information queried from the database is not directly put into the results, but the information is converted into the data that the page needs to display and then stored in the results. This greatly improves the efficiency. You need to use if statements in this process. Next, we will introduce the usage of this sentence in detail in the article. I hope it will be helpful to you.

The usage of if statement

Grammatical structure:

If (expr1,expr2,expr3)

In the above syntax structure, expr1 represents the judgment condition, and expr2 and expr3 are the custom results that conform to expr1.

When the value of expr1 is true, the return value is expr2;. When the value of expr1 is false, the return value is expr3.

Example:

SELECT IF (TRUE,1+1,1+2);-> 2SELECT IF (FALSE,1+1,1+2);-> 3SELECT IF (STRCMP (111,222), "unequal", "equal");-> unequal

Find out the books with a price of 30. If they are PHP books, they should be marked as sold out, otherwise they will be marked with goods.

Select *, if (book_name='PHP',' has been sold out','in stock') as product_status from book where price = 30

Note: the if function can be used when there are only two cases in the table

IF NULL expression

Grammatical structure:

IF NULL (expr1, expr2)

It means that expr1 is returned if the value of expr1 is not NULL, otherwise expr2 is returned, as follows:

SELECT IFNULL (NULL, "11");-> 11SELECT IFNULL ("00", "11");-> 00 about how to use the if statement in sql is shared here. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

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