Dans le langage SQL, il est possible d’utiliser des conditions de type “si / sinon” pour retourner un résultat en fonction de plusieurs possibilités.
Le CASE peut être utilisé dans n’importe quelle instruction ou clause, telle que SELECT, UPDATE, DELETE, WHERE, ORDER BY ou HAVING.
Dans l'exemple suivant affichant le nombre d'articles par catégorie dans un ALIAS "Nombre d'articles" :
COUNT(p_id)) est supérieur à 0, on l'afficheSELECT
b_category.`c_id`,
`c_title`,
(CASE
WHEN COUNT(p_id) > 0 THEN COUNT(p_id)
ELSE 'Aucun article'
END) AS "Nombre d'articles"
FROM `b_category`
LEFT JOIN b_post ON b_post.c_id = b_category.c_id
GROUP BY b_category.c_id
Résultat