php mysql行依赖检查

My MySQL structure is:

id Ergebnis gewinn multi
1  gewonnen 2      8550
2  verloren 0      8550
3  gewonnen 2      6990
4  gewonnen 5      6990
5  gewonnen 12     1443
6  verloren 0      2201

I need to get values from gewinn counted together where multi is same, but only when ergebnis == gewonnen (for all with same multi-value), if one or all ergebnis = verloren the total multi == verloren. so for this example the correct output will be:

8550 == verloren / 0
6990 == gewonnen / 7
1443 == gewonnen / 12
2201 == verloren / 0

Consider the following...

CREATE TABLE my_table
(id INT NOT NULL AUTO_increment primary key
, result varchar(12)
, winnings int
, multi int
);

insert into my_table values
(1  ,'win', 2    ,  8550),
(2  ,'lose', 0    ,  8550),
(3  ,'win', 2    ,  6990),
(4  ,'win', 5     , 6990),
(5  ,'win', 12    , 1443),
(6 , 'lose', 0    ,  2201);

select x.result
     , sum(z.winnings) total
     , x.multi
  from my_table x
  join 
     ( SELECT MULTI
            , MAX(ID) max_id
         FROM MY_table
        group 
           by multi
     ) y
    on y.multi = x.multi
   and y.max_id = x.id
  join my_table z
    on z.multi = x.multi
   and z.result = x.result
 group
    by x.multi, x.result;
+--------+-------+-------+
| result | total | multi |
+--------+-------+-------+
| win    |    12 |  1443 |
| lose   |     0 |  2201 |
| win    |     7 |  6990 |
| lose   |     0 |  8550 |
+--------+-------+-------+

Fiddle for same: http://sqlfiddle.com/#!9/9a31e/3

EDIT: A crude extension to this idea - to return lose/0 if a multi incurs any loss...

SELECT COALESCE(b.result,a.result) result
     , COALESCE(b.winnings,a.total) total
     , a.multi
  FROM 
     ( 
     select x.result
     , sum(z.winnings) total
     , x.multi
  from my_table x
  join 
     ( SELECT MULTI
            , MAX(ID) max_id
         FROM MY_table
        group 
           by multi
     ) y
    on y.multi = x.multi
   and y.max_id = x.id
  join my_table z
    on z.multi = x.multi
   and z.result = x.result
 group
    by x.multi, x.result
    ) a
  LEFT JOIN my_table b
  ON b.multi = a.multi
  AND b.result = 'lose';

if u change the insert into:

insert into my_table values
 (1  ,'lose', 0    ,  8550),
 (2  ,'win', 2    ,  8550),
(3  ,'win', 2    ,  6990),
(4  ,'win', 5     , 6990),
(5  ,'win', 12    , 1443),
(6 , 'lose', 0    ,  2201);

the result is for e.g.: 8550 -> win but should be lose. in case of any id with same multi == lose the complete multi should be result lose