vendredi 4 septembre 2015

MYSQL SELECT with condition

I'm trying to select the sum of the values in the isOK column for each Name seperated, BUT only if isOK = 1 on Day = 2.

The query for the following example table tablename

Name | Day | isOK  
char | int | int   
-----------------
Flo  |   1 |    1  
Seb  |   1 |    1  
Tim  |   1 |    0  
Flo  |   2 |    1  
Seb  |   2 |    0  
Tim  |   2 |    1  

should give Flo: 2 and Tim: 1, but not Seb: 1, since his isOK von Day = 2 is 0.
I've tried using SUM(isOK) with IF constructs, but it's just not working. My alternative solution, to select all Name where isOK = 1 first and select the SUM(isOK) for each of the names is slow and seems in need of improvement.
I guess it's not that difficult, but I've been trying for hours now and I just can't combine my two queries into one.
Thank you!

(sorry for the ugly table, guess I'm not allowed to post images yet)

Aucun commentaire:

Enregistrer un commentaire