#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Question MySQL select - count/ group


    Hi everybody,

    I have a database with a table like this:

    -------------------------------------------
    Unit | Type | Completed
    -------------------------------------------
    Unit 1 | Type 1 | Yes
    Unit 2 | Type 1 | Yes
    Unit 3 | Type 1 | No
    Unit 4 | Type 2 | Yes
    Unit 5 | Type 2 | No
    -------------------------------------------

    I'm working on a query that allows me to echo/ print the following table:

    -------------------------------------------------------------
    Type | Completed = Yes or No | Completed = Yes
    -------------------------------------------------------------
    Type 1 | 3 | 2
    Type 2 | 2 | 1
    -------------------------------------------------------------

    So I guess it's about 'count' and 'group' but I do not know how to write the query.

    As you can see, I'm really new to MySQL/ PHP. :-o

    Thank you very much for your help. :-)

    Markus
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2008
    Posts
    1,927
    Rep Power
    378
    Code:
    SELECT Type
         , COUNT(*) Total
         , SUM(CASE WHEN Completed='Yes' THEN 1 ELSE 0 END) Completed 
      FROM my_table 
     GROUP 
        BY Type;

IMN logo majestic logo threadwatch logo seochat tools logo