Return to Snippet

Revision: 44284
at April 9, 2011 01:30 by dsoms


Updated Code
SELECT
  id,
  sum(if (`user_type` = 1, 1, 0)) as COUNT_TYPE1,
  sum(if (`user_type` = 2, 1, 0)) as COUNT_TYPE2,
  sum(if (`user_type` = 3, 1, 0)) as COUNT_TYPE3,
  sum(if (`user_type` = 4, 1, 0)) as COUNT_TYPE4
FROM users
GROUP BY id

Revision: 44283
at April 9, 2011 01:29 by dsoms


Initial Code
SELECT
  id,
  sum(if (type = 1, 1, 0)) as COUNT_TYPE1,
  sum(if (type = 2, 1, 0)) as COUNT_TYPE2,
  sum(if (type = 3, 1, 0)) as COUNT_TYPE3,
  sum(if (type = 4, 1, 0)) as COUNT_TYPE4
FROM users
GROUP BY id

Initial URL

                                

Initial Description

                                

Initial Title
Countif in MySQL

Initial Tags
mysql

Initial Language
MySQL