Ma requête SQL renvoie Erreur de syntaxe près de '== 1
ma requête est
SELECT COUNT( IF ( application_type== 1 ) ) as total from table_1
4 Réponses :
cas d'utilisation lorsque
SELECT count(if(application_type=1, 1, NULL)) as total from table_1
OU
SELECT COUNT(*) as total from table_1 where application_type=1
OR
SELECT COUNT(case when application_type=1 then 1 end) as total from table_1
Pouvez-vous expliquer pourquoi j'ai défini puis 1 ?
@mrmithun, vous devez utiliser une valeur ou un nom de champ - vous pouvez également utiliser application_type
Votre condition IF (application_type == 1) renvoie en fait TRUE ou FALSE qui vaut 1 OU 0. Maintenant, si vous appliquez COUNT sur une colonne contient 1 OR 0, il vous renverra toujours le nombre total de ROW de la table . Vous devez donc ajuster votre requête. Quoi qu'il en soit, utiliser SUM au lieu de COUNT servira votre objectif, je pense -
SELECT SUM(IF( application_type = 1)) AS total from table_1
Plusieurs problèmes avec votre syntaxe. Cela fonctionne:
SELECT COUNT(*) as total from table_1 WHERE application_type = 1;
Expliquez-lui ce qui n'allait pas avec sa syntaxe, pour qu'il ne fasse pas les mêmes erreurs à l'avenir.
N'utilisez pas du tout if () ou case . MySQL traite commodément les booléens comme des entiers, vous pouvez donc simplement faire:
SELECT SUM( application_type = 1 ) as total FROM table_1;
Bien sûr, si c'est tout ce que vous voulez, utilisez COUNT (*) et déplacez le condition à une clause WHERE .
SELECT COUNT (IF (application_type = 1, 1, 0)) comme total de la table_1