Home Database Mysql Tutorial MySQL语句中如何灵活对if例子进行运用

MySQL语句中如何灵活对if例子进行运用

Jun 07, 2016 pm 04:11 PM
mysql example how article flexible statement use conduct

以下的文章主要描述的是MySQL语句中如何用if例子的实际操作步骤,我们先是以相关代码的方式来引出MySQL语句中如何用if例子的实际操作,以下就是文章的具体操作内容的描述,望你会有所收获。 *,if( sva = 1 ,男,女)asssvafromtanamewheresva 12.2. 控制流程函

以下的文章主要描述的是MySQL语句中如何用if例子的实际操作步骤,我们先是以相关代码的方式来引出MySQL语句中如何用if例子的实际操作,以下就是文章的具体操作内容的描述,望你会有所收获。

<ol class="dp-xml"><li class="alt"><span><span>*,if(</span><span class="attribute">sva</span><span>=</span><span class="attribute-value">1</span><span>,"男","女") as ssva from taname where sva</span><span class="tag"><span class="tag">></span><span>"" </span></span></span></li></ol>
Copy after login

12.2. 控制流程函数

<ol class="dp-xml"><li class="alt"><span><span>CASE value WHEN [compare-value] THEN result [WHEN [compare-value] THEN result ...] <br>[ELSE result] END CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END  </span></span></li></ol>
Copy after login

在第一个方案的返回结果中, value=compare-value。而第二个方案的返回结果是第一种情况的真实结果。如果没有匹配的结果值,则返回结果为ELSE后的结果,如果没有ELSE 部分,则返回值为 NULL。

<ol class="dp-xml">
<li class="alt"><span><span>MySQL(和PHP搭配之最佳组合)</span><span class="tag">></span><span> SELECT CASE 1 WHEN 1 THEN 'one'  </span></span></li>
<li>
<span>-</span><span class="tag">></span><span> WHEN 2 THEN 'two' ELSE 'more' END;  </span>
</li>
<li class="alt">
<span>-</span><span class="tag">></span><span> 'one'  </span>
</li>
<li>
<span>MySQL(和PHP搭配之最佳组合)</span><span class="tag">></span><span> SELECT CASE WHEN 1</span><span class="tag">></span><span>0 THEN 'true' ELSE 'false' END;  </span>
</li>
<li class="alt">
<span>-</span><span class="tag">></span><span> 'true'  </span>
</li>
<li>
<span>MySQL(和PHP搭配之最佳组合)</span><span class="tag">></span><span> SELECT CASE BINARY 'B'  </span>
</li>
<li class="alt">
<span>-</span><span class="tag">></span><span> WHEN 'a' THEN 1 WHEN 'b' THEN 2 END;  </span>
</li>
<li>
<span>-</span><span class="tag">></span><span> NULL  </span>
</li>
</ol>
Copy after login

一个CASE表达式的默认返回值类型是任何返回值的相容集合类型,但具体情况视其所在语境而定。如果用在字符串语境中,则返回结果味字符串。如果用在数字语境中,则返回结果为十进制值、实值或整数值。

<ol class="dp-xml"><li class="alt"><span><span>IF(expr1,expr2,expr3)  </span></span></li></ol>
Copy after login

如果 expr1 是TRUE (expr1 0 and expr1 NULL),则 MySQL语句中用ifIF()的返回值为expr2; 否则返回值则为 expr3。IF() 的返回值为数字值或字符串值,具体情况视其所在语境而定。

<ol class="dp-xml">
<li class="alt"><span><span>MySQL(和PHP搭配之最佳组合)</span><span class="tag">></span><span> SELECT IF(1</span><span class="tag">></span><span>2,2,3);  </span></span></li>
<li>
<span>-</span><span class="tag">></span><span> 3  </span>
</li>
<li class="alt">
<span>MySQL(和PHP搭配之最佳组合)</span><span class="tag">></span><span> SELECT IF(1</span><span class="tag"><span class="tag-name">2</span><span>,'yes ','no');  </span></span>
</li>
<li>
<span>-</span><span class="tag">></span><span> 'yes'  </span>
</li>
<li class="alt">
<span>MySQL(和PHP搭配之最佳组合)</span><span class="tag">></span><span> SELECT IF(STRCMP('test','test1'),'no','yes');  </span>
</li>
<li>
<span>-</span><span class="tag">></span><span> 'no'  </span>
</li>
</ol>
Copy after login

如果expr2 或expr3中只有一个明确是 NULL,则MySQL语句中用ifIF() 函数的结果类型 为非NULL表达式的结果类型。

expr1 作为一个整数值进行计算,就是说,假如你正在验证浮点值或字符串值, 那么应该使用比较运算进行检验。

<ol class="dp-xml">
<li class="alt"><span><span>MySQL(和PHP搭配之最佳组合)</span><span class="tag">></span><span> SELECT IF(0.1,1,0);  </span></span></li>
<li>
<span>-</span><span class="tag">></span><span> 0  </span>
</li>
<li class="alt">
<span>MySQL(和PHP搭配之最佳组合)</span><span class="tag">></span><span> SELECT IF(0.1</span><span class="tag"><span class="tag">></span><span>0,1,0);  </span></span>
</li>
<li>
<span>-</span><span class="tag">></span><span> 1  </span>
</li>
</ol>
Copy after login

在所示的第一个例子中,IF(0.1)的返回值为0,原因是 0.1 被转化为整数值,从而引起一个对 IF(0)的检验。这或许不是你想要的情况。在第二个例子中,比较检验了原始浮点值,目的是为了了解是否其为非零值。比较结果使用整数。

MySQL语句中用ifIF() (这一点在其被储存到临时表时很重要 ) 的默认返回值类型按照以下方式计算:

表达式

返回值

expr2 或expr3 返回值为一个字符串。

字符串

expr2 或expr3 返回值为一个浮点值。

浮点

expr2 或 expr3 返回值为一个整数。

整数

假如expr2 和expr3 都是字符串,且其中任何一个字符串区分大小写,则返回结果是区分大小写。http://blog.knowsky.com/

IFNULL(expr1,expr2)

假如expr1 不为 NULL,则 IFNULL() 的返回值为 expr1; 否则其返回值为 expr2。IFNULL()的返回值是数字或是字符串,具体情况取决于其所使用的语境。

<ol class="dp-xml">
<li class="alt"><span><span>MySQL(和PHP搭配之最佳组合)</span><span class="tag">></span><span> SELECT IFNULL(1,0);  </span></span></li>
<li>
<span>-</span><span class="tag">></span><span> 1  </span>
</li>
<li class="alt">
<span>MySQL(和PHP搭配之最佳组合)</span><span class="tag">></span><span> SELECT IFNULL(NULL,10);  </span>
</li>
<li>
<span>-</span><span class="tag">></span><span> 10  </span>
</li>
<li class="alt">
<span>MySQL(和PHP搭配之最佳组合)</span><span class="tag">></span><span> SELECT IFNULL(1/0,10);  </span>
</li>
<li>
<span>-</span><span class="tag">></span><span> 10  </span>
</li>
<li class="alt">
<span>MySQL(和PHP搭配之最佳组合)</span><span class="tag">></span><span> SELECT IFNULL(1/0,'yes');  </span>
</li>
<li>
<span>-</span><span class="tag">></span><span> 'yes'  </span>
</li>
</ol>
Copy after login

IFNULL(expr1,expr2)的默认结果值为两个表达式中更加“通用”的一个,顺序为STRING、 REAL或 INTEGER。假设一个基于表达式的表的情况, 或MySQL(和PHP搭配之最佳组合)必须在内存储器中储存一个临时表中MySQL语句中用ifIFNULL()的返回值:

CREATE TABLE tmp SELECT IFNULL(1,'test') AS test;

在这个例子中,测试列的类型为 CHAR(4)。

NULLIF(expr1,expr2)

如果expr1 = expr2 成立,那么返回值为NULL,否则返回值为 expr1。这和CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END相同。

<ol class="dp-xml">
<li class="alt"><span><span>MySQL(和PHP搭配之最佳组合)</span><span class="tag">></span><span> SELECT NULLIF(1,1);  </span></span></li>
<li>
<span>-</span><span class="tag">></span><span> NULL  </span>
</li>
<li class="alt">
<span>MySQL(和PHP搭配之最佳组合)</span><span class="tag">></span><span> SELECT NULLIF(1,2);  </span>
</li>
<li>
<span>-</span><span class="tag">></span><span> 1  </span>
</li>
</ol>
Copy after login

注意,如果参数不相等,则 MySQL(和PHP搭配之最佳组合) 两次求得的值为 expr1 。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

See all articles