MySQL syntax one: When the case is detailed

Source: Internet
Author: User

Defined:

The first type:

Case Case_value while    When_value then statement_list    [when When_value then Statement_list]    ... [ELSE Statement_list] END case

The second type:

When the case is    search_condition then statement_list    [when Search_condition then Statement_list]    ... [ELSE Statement_list] END case

  

Syntax Explanation:

The Case_value after the case can be any value, or it can be a field. When_value is the value that is to be compared, if case_value and When_value are equal, the value is statement, or null if not equal (if there is else, how to show the statement after else),

In fact, the function of the whole statement is a bit like swith case;

Example 1:

Existing table Event_register (a random table, examples are not particularly suitable, mainly used to illustrate the use of)

SELECT name, Case Namewhen ' Zhang San ' then 1 when ' small plainly ' then 2ELSE 3END as AAA from Event_register where  order_id = 21312529

Operation Result:

Name Aaa
Wowapps 2
Tom 1
Little Black 3
Small Black 23 3

Example 2:

SELECT name, case if name = ' Zhang San ' then 1 when name = ' small plainly ' then 2else 3END as AAA from Event_register where  order_id = 21312529

Operation Result:

Name Aaa
Wowapps 2
Tom 1
Little Black 3
Small Black 23 3

The Case_value is removed, and the value of When_value becomes an expression , which is generally the same.

MySQL syntax one: When the case is detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.