How to SELECT count multiple values

Discussion in 'MySQL' started by anilinkz, Sep 3, 2010.

  1. #1
    i have a table

    how do i SELECT so it will be grouped by section and count how many names in that gender



    TIA. :eek:
     
    anilinkz, Sep 3, 2010 IP
  2. cDc

    cDc Peon

    Messages:
    127
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try something like SELECT count(*), gender FROM mytable GROUP BY gender
     
    cDc, Sep 4, 2010 IP
    anilinkz likes this.
  3. anilinkz

    anilinkz Peon

    Messages:
    118
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    nop. not what i needed... anyone?
     
    anilinkz, Sep 4, 2010 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    That query should work. What is it returning, and what do you need it to return?
     
    jestep, Sep 7, 2010 IP
  5. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Sounds like you want to group this by both section and gender and it looks like you want the values of the gender field to become column names. MySQL doesn't support these type of queries (cross-tab) so you have to manually do it. As long as you just have 4 genders use this:


    SELECT section, SUM(IF (gender="male", 1,0)) AS countmale, SUM(IF (gender="female", 1,0)) AS countfemale, SUM(IF (gender="gay", 1,0)) AS countgay, SUM(IF (gender="lesbian", 1,0)) AS countlesbian FROM mytable GROUP BY section;
    PHP:

    If you have more genders you need to paste in more of these: 'SUM(IF (gender="male", 1,0)) AS countmale'
     
    plog, Sep 8, 2010 IP
    anilinkz likes this.
  6. anilinkz

    anilinkz Peon

    Messages:
    118
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    @plog
    that did it. thanks. +rep ^_^
     
    anilinkz, Sep 16, 2010 IP