A method to implement multi-condition query using case-when statement in Mysql _mysql

Source: Internet
Author: User
Examples are as follows:

Data table is demotable, field has ID, condition1,condition2,condition3,condition4,condition5

The requirement is to query demotable, condition1,condition2,condition3,condition4,condition5 Five fields that meet any of two or more than two conditions.

You can use case when to implement this condition, requiring nested subqueries

Examples of SQL statement code are as follows:
Copy Code code as follows:

SELECT * from Demotable
WHERE (SELECT Case 1 when condition1 satisfies the condition THEN 1 ELSE 0 end from demotable)
+ (SELECT Case 1 when condition2 satisfies the condition THEN 1 ELSE 0 end from demotable)
+ (SELECT Case 1 when Condition3 satisfies the condition THEN 1 ELSE 0 end from demotable)
+ (SELECT Case 1 when condition4 satisfies the condition THEN 1 ELSE 0 end from demotable)
+ (SELECT Case 1 when condition5 satisfies the condition THEN 1 ELSE 0 end from demotable) >=2
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.