简体   繁体   English

在MySQL的单行CASE…WHEN语句中使用多个条件

[英]Using multiple conditions in a single line of CASE…WHEN statement in MySQL

Is there any way of using multiple conditions inside a single line of CASE...WHEN statement in MySQL? 有没有办法在MySQL的CASE ... WHEN语句的一行内使用多个条件?

For instance, I want to write a query similar to the following: 例如,我要编写类似于以下内容的查询:

SELECT col1, col2,
CASE 
    WHEN ((condition1) AND (condition2)) THEN 1 ELSE 0
END as col3,
col4
FROM table1

How can I write a query similar to the above one appropriately in MySQL? 如何在MySQL中适当地编写类似于上述查询的查询?

The MySQL statement you have provided looks right. 您提供的MySQL语句看起来正确。 Another alternative would be using IF 另一种选择是使用IF

SELECT col1, col2, IF((condition1) AND (condition2), 1, 0), col4 from table1;

For more help you could refer to : MySQL Reference Guide 有关更多帮助,请参考: MySQL参考指南

SELECT col1, col2,
CASE 
    WHEN (if(your_condition , 'true_Result' , 'False_result') AND if(your_condition , 'true_Result' , 'False_result')) THEN 1 ELSE 0
END as col3,
col4
FROM table1

Explanation: if(your_condition , 'true_Result' , 'False_result') If your conditions becomes true, it would pick your value set in 'true_result', if condition is false, it would pick from 'false_Result' Example: if(sum(columnname)=0 , 1 , 0) to check if a particular column contains all values as 0 or not. 说明:if(your_condition,'true_Result','False_result')如果条件为真,则将选择在'true_result'中设置的值,如果条件为false,则将从'false_Result'中选择示例: if(sum(columnname)=0 , 1 , 0)检查特定列是否包含所有值为0的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM