使用case语句编写子查询

时间:2018-02-02 10:40:15

标签: php mysql

来自此查询

SELECT *, 
CASE WHEN STATUS = ONE THEN STATUS 
WHEN STATUS = TWO THEN STATUS 
WHEN ONE = TWO THEN ONE 
ELSE NULL END answer FROM TABLE1

我如何根据“答案”获取行,假设我想要一个像where的条件, ...where answer='yes',我该如何编写该子查询

EDIT1:

上述查询的示例表输出:

|STATUS |ONE    |TWO    |answer
|NO     |NO     |YES    |NO
|YES    |YES    |NO     |YES
|NO     |NO     |NO     |NO

预期输出,条件为“回答”(例如,答案='是')

|STATUS |ONE    |TWO    |answer
|YES    |YES    |NO     |YES

1 个答案:

答案 0 :(得分:0)

您没有发布表格结构,但根据您的示例输出,我假设有3列名为statusonetwo,如{{3}中所示}

如果是这样的话。以下查询可能对您有用。

SELECT *, 
CASE WHEN status = one THEN status
WHEN status = two THEN status
WHEN one = two THEN one 
ELSE NULL END answer 
FROM `TABLE1` 
HAVING answer='YES';