sql >> Base de Datos >  >> RDS >> Mysql

MySQL suma basada en el valor de col

Podría usar la agregación condicional:

SELECT fruit, SUM(CASE `a-b-c`
                     WHEN 'a' THEN 1
                     WHEN 'b' THEN 2
                     WHEN 'c' THEN 3
                  END) AS total 
FROM tbl_fruits
GROUP BY fruit;

SqlFiddleDemo

Salida:

╔═════════╦═══════╗
║ fruit   ║ total ║
╠═════════╬═══════╣
║ banana  ║     3 ║
║ orange  ║     7 ║
╚═════════╩═══════╝

Alternativamente usando ELT/FIELD :

SELECT fruit, SUM(ELT(FIELD(`a-b-c`,'a','b','c'),1,2,3)) AS total 
FROM tbl_fruits
GROUP BY fruit;

SqlFiddleDemo2