The case when statement condition query method in MySQL

Source: Internet
Author: User
Tags mysql tutorial

Case
Calculate the condition list and return one of multiple possible result expressions.

Case has two formats:

The simple case function compares an expression with a group of simple expressions to determine the result.


The case search function calculates a group of boolean expressions to determine the result.
Both formats support the optional else parameter.

Syntax
Simple case functions:

Case input_expression
When when_expression then result_expression
[... N]
[
Else else_result_expression
End

Case search function:

Case
When boolean_expression then result_expression
[... N]
[
Else else_result_expression
End


For example, the following statement shows the Chinese year and month
Select getdate () as date, case month (getdate ())
When 11 then '11'
When 12 then '12'
Else substring ('february 5, 1234, 80 or 90 ', month (getdate (), 1)
End + 'month' as month

See the instance below


The data table is demotable. The fields include id, condition1, condition2, condition3, condition4, and condition5.

The requirement is to query the content of any two or more conditions in demotable, including condition1, condition2, condition3, condition4, and condition5.

You can use case when to implement this condition. nested subquery statements are required.

The SQL statement code example is as follows:
Copy the Code as follows:
Select * from demotable
Where (select case 1 when condition1 meets conditions then 1 else 0 end from demotable)
+ (Select case 1 when condition2 meets conditions then 1 else 0 end from demotable)
+ (Select case 1 when condition3 meets conditions then 1 else 0 end from demotable)
+ (Select case 1 when condition4 meets conditions then 1 else 0 end from demotable)
+ (Select case 1 when condition5 meets conditions then 1 else 0 end from demotable)> = 2


The default Return Value Type of a case expression is the compatible set type of any returned value, but the specific situation depends on the context. If it is used in a string context, a result flavor string is returned. If it is used in the numeric context, the returned result is a decimal value, a real value, or an integer.

If (expr1, expr2, expr3)

If expr1 is true (expr1 <> 0 and expr1 <> null), the return value of if () is expr2; otherwise, the return value is expr3. The return value of if () is a numeric or string value, depending on the context.

Mysql tutorial> select if (1> 2, 2, 3 );

-> 3

Mysql> select if (1 <2, 'yes', 'no ');

-> 'Yes'

Mysql> select if (strcmp ('test', 'test1'), 'No', 'yes ');

-> 'No'


In order to use case in the group by block, the query statement must repeat the case block in the select block in the group by block.

 

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.