Case usage in MySQL

Source: Internet
Author: User

Reprint please specify the source:

The simple case statement only allows you to match the value of an expression to a different set of values. In order to perform more complex matches, such as scopes, you can use the searchable case statement.
A searchable case statement is equivalent to an if statement, but its construction is more readable. The following instructions can be used to search the syntax of a case statement:
Case
When condition_1 then commands
When condition_2 then commands
...
ELSE commands
END case;

MySQL evaluates each condition in the evaluate when clause until it finds a condition with a value of true, and then executes the corresponding command (commands) in the then clause.
If no condition is true, the command in the ELSE clause is executed (commands). If you do not specify an ELSE clause, and no condition is true,mysql, an error message is issued.

Instance:

SELECT Case U.playertype when'1'Then U.num ELSE0END as'Liveplaycount', Case U.playertype when'2'Then U.num ELSE0END as'Vodplaycount', Case U.playertype when'1'Then U.flownum ELSE0END as'Liveflow', Case U.playertype when'2'Then U.flownum ELSE0END as'Vodflow',0As'Storespace'                , 0As'Storeresiduespace'From (SELECT playertype, COUNT (*) as NUM, Ifnull (SUM (bytessend),0) as'Flownum'From tbl_player_statistics WHERE playertime between CONCAT (Date_format (#{month},'%y-%m'),'-01 00:00:00') and CONCAT (Date_format (Last_day (#{month}),'%y-%m-%d'),'23:59:59') GROUP by Playertype)

Case usage in MySQL

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.