Home > Database > Mysql Tutorial > body text

What is the usage of mysql case when

coldplay.xixi
Release: 2020-10-12 13:58:56
Original
5522 people have browsed it

The usage of mysql case when is: 1. Used as a simple search, the syntax is [CASE [col_name] WHEN [value1]]; 2. Used as a search function, the syntax is [CASE WHEN [expr] THEN [ result1]].

What is the usage of mysql case when

【Related learning recommendations: mysql tutorial(Video)】

The usage of mysql case when is:

There are two syntaxes for case when

Simple function

CASE [col_name] WHEN [value1] THEN [result1]…ELSE [default] END
Copy after login
Copy after login

Search function

CASE WHEN [expr] THEN [result1]…ELSE [default] END
Copy after login
Copy after login

What is the difference between these two syntaxes?

1. Simple function

CASE [col_name] WHEN [value1] THEN [result1]…ELSE [default] END
Copy after login
Copy after login

Enumerate all possible values ​​of this field*

SELECT
    NAME '英雄',
    CASE NAME
        WHEN '德莱文' THEN
            '斧子'
        WHEN '德玛西亚-盖伦' THEN
            '大宝剑'
        WHEN '暗夜猎手-VN' THEN
            '弩'
        ELSE
            '无'
    END '装备'
FROM
    user_info;
复制代码
复制代码
SELECT
    NAME '英雄',
    CASE NAME
        WHEN '德莱文' THEN
            '斧子'
        WHEN '德玛西亚-盖伦' THEN
            '大宝剑'
        WHEN '暗夜猎手-VN' THEN
            '弩'
        ELSE
            '无'
    END '装备'
FROM
    user_info;
Copy after login

2. Search function

CASE WHEN [expr] THEN [result1]…ELSE [default] END
Copy after login
Copy after login

The search function can write judgments, and the search function will only return the first value that meets the conditions, and other cases are ignored

# when 表达式中可以使用 and 连接条件
SELECT
    NAME '英雄',
    age '年龄',
    CASE
        WHEN age < 18 THEN
            &#39;少年&#39;
        WHEN age < 30 THEN
            &#39;青年&#39;
        WHEN age >= 30
        AND age < 50 THEN
            &#39;中年&#39;
        ELSE
            &#39;老年&#39;
    END &#39;状态&#39;
FROM
    user_info;
Copy after login
聚合函数 sum 配合 case when 的简单函数实现行转列
SELECT
    st.stu_id &#39;学号&#39;,
    st.stu_name &#39;姓名&#39;,
    sum(
        CASE co.course_name
        WHEN &#39;大学语文&#39; THEN
            sc.scores
        ELSE
            0
        END
    ) &#39;大学语文&#39;,
    sum(
        CASE co.course_name
        WHEN &#39;新视野英语&#39; THEN
            sc.scores
        ELSE
            0
        END
    ) &#39;新视野英语&#39;,
    sum(
        CASE co.course_name
        WHEN &#39;离散数学&#39; THEN
            sc.scores
        ELSE
            0
        END
    ) &#39;离散数学&#39;,
    sum(
        CASE co.course_name
        WHEN &#39;概率论与数理统计&#39; THEN
            sc.scores
        ELSE
            0
        END
    ) &#39;概率论与数理统计&#39;,
    sum(
        CASE co.course_name
        WHEN &#39;线性代数&#39; THEN
            sc.scores
        ELSE
            0
        END
    ) &#39;线性代数&#39;,
    sum(
        CASE co.course_name
        WHEN &#39;高等数学&#39; THEN
            sc.scores
        ELSE
            0
        END
    ) &#39;高等数学&#39;
FROM
    edu_student st
LEFT JOIN edu_score sc ON st.stu_id = sc.stu_id
LEFT JOIN edu_courses co ON co.course_no = sc.course_no
GROUP BY
    st.stu_id
ORDER BY
    NULL;
Copy after login

Want to know more For programming learning, please pay attention to the php training column!

The above is the detailed content of What is the usage of mysql case when. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!