SQL选择列多次具有不同的条件

时间:2015-09-12 08:14:08

标签: mysql inner-join aggregate-functions

我有一个包含事件的表,其中包含事件中受伤人数的列以及该事件中被杀死人数的列。事件表与参与者表链接,您可以在其中找到人员年龄的列,并且还有另一个表参与者类型,其中包含类型列(man(id = 1),woman(id = 2))。 所以,我想要检索受伤的列号,列号被杀死,其中类型是男性,并且在他们旁边列号受伤,列号被杀死,其中类型是女性,所有组都按年龄。

select    
ip.age as age, 
sum(i.number_injured) as injured,
sum(i.number_killed) as killed
from Incidents i
inner join Participants ip on i.id = ip.incident_id
inner join ParticipantTypes ipt on ip.type_id = ipt.id
where ipt.id = 1
group by ip.age

以下查询仅对ParticipantTypes.type = 1(man)

起作用并检索结果

我想要它像

select    
ip.age as age, 
sum(i.number_injured) as injured where ipt.id = 1,
sum(i.number_killed) as killed where ipt.id = 1, 
sum(i.number_injured) as injured where ipt.id = 2,
sum(i.number_killed) as killed where ipt.id = 2, 
from Incidents i
inner join Participants ip on i.id = ip.incident_id
inner join ParticipantTypes ipt on ip.type_id = ipt.id
group by ip.age

4 个答案:

答案 0 :(得分:2)

select    
    ip.age as age, 
    sum(case when ipt.id=1 then i.number_injured    else 0 end)     as injured_man,
    sum(case when ipt.id=1 then i.number_killed     else 0 end)     as killed_man,
    sum(case when ipt.id=2 then i.number_injured    else 0 end)     as injured_woman,
    sum(case when ipt.id=2 then i.number_killed     else 0 end)     as killed_woman
 from 
    Incidents i
inner join 
    Participants ip on i.id = ip.incident_id
inner join 
    ParticipantTypes ipt on ip.type_id = ipt.id
group by ip.age

示例结果:

age         injured_man killed_man  injured_woman killed_woman 
----------- ----------- ----------- ------------- ------------ 
         30          10           1             3            1 
         31          12           1             1            6 
         32          14           2             4            4 

答案 1 :(得分:1)

您正在womanipt.id = 1过滤掉inner join条件where的{​​{1}}条记录。

此外,正如我从问题中所理解的那样,您按age进行分组,这不是您想要的。所以,你的最终查询应该是。

编辑:根据相关更新的详细信息,以下是查询

select
    ip.age,
    sum(case when ipt.id =1 then i.number_injured else 0 end) as men_injured,
    sum(case when ipt.id =1 then i.number_killed else 0 end) as men_killed,
    sum(case when ipt.id =2 then i.number_injured else 0 end) as women_injured,
    sum(case when ipt.id =2 then i.number_killed else 0 end) as women_killed
from Incidents i
       inner join Participants ip on i.id = ip.incident_id
       inner join ParticipantTypes ipt on ip.type_id = ipt.id
group by ip.age

答案 2 :(得分:1)

select    
ip.age as age, 
sum(if(ipt.id=1,i.number_injured,0)) as injured_by_man,
sum(if(ipt.id=1,i.number_killed,0) as killed_by_man,
sum(if(ipt.id=2,i.number_injured,0)) as injured_by_woman,
sum(if(ipt.id=2,i.number_killed,0)) as killed_by_woman

from Incidents i
inner join Participants ip on i.id = ip.incident_id
inner join ParticipantTypes ipt on ip.type_id = ipt.id
group by ip.age

注意:上述代码未经过测试。它可能包含小的语法错误。

答案 3 :(得分:1)

试试这个......

select    
ip.age as age, 
sum(case when ipt.id=1then i.number_injured else 0                
end) as injured_by_man,
sum(case when ipt.id=1then i.number_killed else 0  
end) as killed_by_man,
sum(case when ipt.id=2 then i.number_injured else 0 
end) as injured_by_woman,
sum(case when ipt.id=2 then i.number_killed else 0 
end) as killed_by_woman

from Incidents i
inner join Participants ip on i.id = ip.incident_id
inner join ParticipantTypes ipt on ip.type_id = ipt.id
group by ip.age