Search code examples
mysqlcountduplicatesvote

Counting votes in MySQL


I have a table, with the following columns: id name vote

Each name is unique, and each vote is either null or contains a name.

I need a MySQL statement to return how many votes each person has, and who voted for each person.

I've been working on this for 3 hours, and I'm at a complete loss, so I honestly don't care how inefficient it is, or how you do it.


Solution

  • How many votes:

    select 
     count(*) as numVotes, 
     vote
    from
     voteTable
    where
     vote IS NOT NULL
    group by 
     vote
    order by
     numVotes desc
    

    Who voted for each:

    Select
     name,
     vote
    from
     voteTable
    

    ... unless I'm misreading something, it should be that simple