Jump to content

PHP Counting


CanMan2004

Recommended Posts

Hi all

I have a sql table which looks like

ID    NAME    USERID
1    Dave      4324566
2    John      1245690
3    Sarah    7896645
4    Dave      4324566
5    Henry    4324566
6    Sarah    1245690
7    Bill          4324566
8    Dave      8754446

What I want to do is to perform a query which will return a list of names with the most popular at the top with the least most popular at the bottom.

The twist is that if a user has added the same name more than once, then it should count it as one, for example, if it was counting the name Dave and the following was stored

ID    NAME    USERID
1    Dave      1245690
2    Dave      4324566
3    Dave      1245690

Then it would count only 2 rows, as the NAME Dave was entered by USERID 1245690 twice.

With the following

ID    NAME    USERID
1    Dave      1245690
2    Dave      4324566
3    Dave      1245690
4    Sarah      3245565

It would return

Dave - 2
Sarah - 1

Does this make sense? I have been racking my brain all day to perform this sort of query, but am totally stuck.

Any help would be ace

Thanks

Ed
Link to comment
Share on other sites

Basically the same query as before w/o the GROUP BY clause (not tested):

SELECT COUNT(temp.*) as count
FROM (SELECT DISTINCT name, userid FROM `tableName`) as temp

By the way, is there any reason you can't just delete the duplicates from the table and add some logic to prevent duplicates in the future. That seems a better approach to me.
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.