Jump to content

[SOLVED] Need help putting together query.


pocobueno1388

Recommended Posts

Okay, I am stumped on how to form my query to get my desired results.

 

I have a table like so:


TABLE 'club_votes'
------------------
vote_for [A players unique ID]
clubID [A clubs unique ID]

 

Here is some example data of what the rows would hold:


Vote_for           ClubID
--------          --------
   1                  1
   10                 1   
   1                  1 
   1                  1 
   10                 2
   5                  2
   6                  2
   6                  2
   1                  2

 

Look at the "vote_for" column, "1" got 4 votes for club 1 since the number appears 4 times.

 

What I am trying to do is display the person who got the most votes for each club.

 

So according to the rows in the table, I would want to output this:

Club           Winner
-----         -------
  1              1
  2              6

 

Any help putting this query together would be greatly appreciated =] Thanks.

Link to comment
Share on other sites

select count(distinct(id)) as result, id from tablename group by id result

 

is this what you mean

your example arent clear enough for me coz its not showing the relationship of the table but i guess that will do

 

or tell me if I'm totally wrong

 

edited.....

 

Link to comment
Share on other sites

that error is due to max()

 

ok, u can try this

 

create temporary table temp as select club_id,vote_for,count(vote_for) as votes  from table group by club_id,vote_for order by club_id;

 

select club_id,vote_for,count(vote_for) as cnt  from table group by club_id,vote_for order by club_id having cnt=any(select max(votes) from temp group by club_id);

 

huh.......there might be a alternative way.

Link to comment
Share on other sites

You need to use SUM().

 

Yeah, I was thinking that, but wouldn't that add all the rows together? I just need to count how many rows for each user voted for, so I don't understand how I would use SUM() in the query. I will keep playing with it to see if I can get it to work.

 

Sorry, I missed the point :-( Each record is a single vote?  Then you need to a do a count() with a group by club_id....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.