mysql - if语句中的if语句

时间:2015-04-23 09:17:51

标签: mysql sql

Name    Value   AnotherColumn
-----------
Pump 1  8000    Something1
Pump 1  1000    Something2
Pump 1  3000    Something3
Pump 2  3043    Something4
Pump 2  4594    Something5
Pump 2  6165    Something6

在此表中,我想在Name列上进行分组,并将输出命名为value_exist。

如果该名称组的任何值列中存在1000,则

value_exist将为1。所以输出将是:

Name    value_exist
-----------
Pump 1  1
Pump 2  0 

3 个答案:

答案 0 :(得分:2)

也许是这样的:

select name, MAX(CASE WHEN value=1000 THEN 1 ELSE 0 END) as value_exist
from your_table
group by name

答案 1 :(得分:1)

还有一个:

select name , max(if(value=1000,1,0)) as value_exist
from table 
group by name;

答案 2 :(得分:0)

select name, sum(value = 1000) as value_exist
from your_table
group by name