Home Database Mysql Tutorial case when then else SQL语句

case when then else SQL语句

Jun 07, 2016 pm 05:40 PM
case else then statement

sql语句判断方法之一 Case具有两种格式。简单Case函数和Case搜索函数。 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END --Case搜索函数 CASE WHEN sex = '1' THEN '男' WHEN sex = '2' THEN '女' ELSE '其他' END 这两种方

sql语句判断方法之一
Case具有两种格式。简单Case函数和Case搜索函数。
--简单Case函数
CASE sex
WHEN '1' THEN '男'
WHEN '2' THEN '女'
ELSE '其他' END
--Case搜索函数
CASE WHEN sex = '1' THEN '男'
WHEN sex = '2' THEN '女'
ELSE '其他' END


这两种方式,可以实现相同的功能。简单Case函数的写法相对比较简洁,但是和Case搜索函数相比,功能方面会有些限制,比如写判断式。
还有一个需要注意的问题,Case函数只返回第一个符合条件的值,剩下的Case部分将会被自动忽略。

例子:
有一张表,里面有3个字段:语文,数学,免备案空间,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路):
大于或等于80表示优秀,美国服务器,大于或等于60表示及格,小于60分表示不及格。
显示格式:
语文 数学 英语
及格 优秀 不及格
------------------------------------------
select
(case when 语文>=80 then '优秀'
when 语文>=60 then '及格'
else '不及格') as 语文,
(case when 数学>=80 then '优秀'
when 数学>=60 then '及格'
else '不及格') as 数学,
(case when 英语>=80 then '优秀'
when 英语>=60 then '及格'
else '不及格') as 英语,
from table


CASE 可能是 SQL 中被误用最多的关键字之一。虽然你可能以前用过这个关键字来创建字段,但是它还具有更多用法。例如,美国服务器,你可以在 WHERE 子句中使用 CASE。

首先让我们看一下 CASE 的语法。在一般的 SELECT 中,其语法如下:

SELECT =
            CASE
            WHEN THEN
            WHEN THEN
            ELSE
            END


在上面的代码中需要用具体的参数代替尖括号中的内容。下面是一个简单的例子:

USE pubs
            GO
            SELECT
            Title,
            'Price Range' =
            CASE
            WHEN price IS NULL THEN 'Unpriced'
            WHEN price             WHEN price BETWEEN 10 and 20 THEN 'Average'
            ELSE 'Gift to impress relatives'
            END
            FROM titles
            ORDER BY price
            GO


这是 CASE 的典型用法,但是使用 CASE 其实可以做更多的事情。比方说下面的 GROUP BY 子句中的 CASE:

SELECT 'Number of Titles', Count(*)
            FROM titles
            GROUP BY
            CASE
            WHEN price IS NULL THEN 'Unpriced'
            WHEN price             WHEN price BETWEEN 10 and 20 THEN 'Average'
            ELSE 'Gift to impress relatives'
            END
            GO


你甚至还可以组合这些选项,添加一个 ORDER BY 子句,如下所示:

USE pubs
            GO
            SELECT
            CASE
            WHEN price IS NULL THEN 'Unpriced'
            WHEN price             WHEN price BETWEEN 10 and 20 THEN 'Average'
            ELSE 'Gift to impress relatives'
            END AS Range,
            Title
            FROM titles
            GROUP BY
            CASE
            WHEN price IS NULL THEN 'Unpriced'
            WHEN price             WHEN price BETWEEN 10 and 20 THEN 'Average'
            ELSE 'Gift to impress relatives'
            END,
            Title
            ORDER BY
            CASE
            WHEN price IS NULL THEN 'Unpriced'
            WHEN price             WHEN price BETWEEN 10 and 20 THEN 'Average'
            ELSE 'Gift to impress relatives'
            END,
            Title
            GO

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

HMD Skyline gets a new color option and official magnetic case HMD Skyline gets a new color option and official magnetic case Aug 23, 2024 am 07:04 AM

When the HMD Skyline(available on Amazon for $499) was launched last month, it was released in two colors - Neon Pink and Twisted Black. They are now joined by a third color dubbed Blue Topaz. HMD Global has also announced an official case for the ph

switch case judgment variable switch case judgment variable Feb 19, 2024 am 08:04 AM

Switchcase requires specific code examples to determine variables. In programming, we often need to perform different operations based on different variable values. The switchcase statement is a convenient structure that allows you to select different blocks of code for execution based on the value of a variable. The following is a specific code example that shows how to use the switchcase statement to determine different values ​​​​of variables: #includeintmain(){

multi-catch statement in PHP8.0 multi-catch statement in PHP8.0 May 14, 2023 pm 01:51 PM

With the development of web applications, PHP language has been widely used in web development. In the PHP8.0 version, a new language feature was introduced - the multi-catch statement. What is a multi-catch statement? In previous PHP versions, developers needed to write multiple catch statements to handle multiple exception types. For example, the following code block shows the handling of two different exceptions: try{//Somecodethatmay

How to implement the statement of inserting data in MySQL? How to implement the statement of inserting data in MySQL? Nov 08, 2023 am 11:48 AM

How to implement the statement of inserting data in MySQL? When using a MySQL database, inserting data is a very basic and common operation. By inserting data, new records can be added to database tables to provide support for business operations. This article will introduce how to use the INSERT statement in MySQL to implement data insertion operations, and provide specific code examples. The INSERT statement in MySQL is used to insert new records into the database table. Its basic syntax format is as follows: INSERTINTOt

To understand flow control statements in Python, you need to master several situations To understand flow control statements in Python, you need to master several situations Jan 20, 2024 am 08:06 AM

Python is a widely used high-level programming language. It is easy to learn, efficient and flexible, and is deeply loved by developers. In Python, flow control statements are an important part of implementing program logic. This article will introduce commonly used flow control statements in Python and provide code examples to deepen understanding. In Python, common flow control statements include conditional statements and loop statements. Conditional statements execute different code blocks based on the true or false condition and are used to determine and select execution branches. The loop statement is used to repeat

How to implement the statement to unlock the table in MySQL? How to implement the statement to unlock the table in MySQL? Nov 08, 2023 pm 06:28 PM

How to implement the statement to unlock the table in MySQL? In MySQL, table locks are a commonly used locking mechanism used to protect data integrity and consistency. When a transaction is reading and writing to a table, other transactions cannot modify the table. This locking mechanism ensures data consistency to a certain extent, but may also cause other transactions to be blocked. Therefore, if a transaction cannot continue for some reason, we need to manually unlock the table so that other transactions can continue. MySQL provides a variety of

What is the usage of else in Python loop structure? What is the usage of else in Python loop structure? Sep 26, 2023 am 10:52 AM

In Python's loop structure, the else block is used to execute a specific piece of code when the loop ends normally. If the loop is interrupted by a break statement, the code in the else block will not be executed. Using else blocks can make the code clearer and easier to understand, and can perform some necessary operations after the loop ends.

Master the types of Python flow control statements and learn from scratch! Master the types of Python flow control statements and learn from scratch! Jan 20, 2024 am 09:02 AM

To learn Python from scratch, first understand the types of flow control statements! Python is a simple and powerful programming language that is widely used in data analysis, artificial intelligence, network development and various scientific computing fields. As a beginner, it is very important to master basic flow control statements, because they are the basis for realizing logical judgment and controlling the program execution flow. In Python, there are three main types of flow control statements: sequential structures, conditional structures and loop structures. The following will introduce these three process control statements in detail and give the corresponding

See all articles