Jump to content

SQL Stuff....


ari_aaron

Recommended Posts

Ok, this is rather complicated, and I'm probably not going to explain it very well....

I have a SQL Table. The 2 relevant columns are unam and points. Points can be a 1 or -1 and unam is a username. I want to get the top 10 amounts of points. The problem is that there is no list of usernames. I somehow need to get a PHP script to get the names of the people with the top 10 amount of points.

Can someone give me some help as to how I would do this?
Link to comment
Share on other sites

Ok, I told you I wouldn't explain it well.

A user can be entered many times. For example:

[code]
|------------------|
|  user      | points|
|ari_aaron  | 1      |
|ari_aaron  | 1      |
|Takagi      | 1      |
|ari_aaron  | -1      |
[/code]
In this case, I have 2 points.
Link to comment
Share on other sites

Ah, yeah I see now. Sorry, I misunderstood your post. I see now what your doing, if there is a negative you want it to drop one of the positive points? I am not really sure how to do that, let me look into it a bit more and hopefully I will be able to come up with a solution for you (if someone else doesn't in the meantime).
Link to comment
Share on other sites

I'm with roopurt18 here :)  But I don't think you should include "DISTINCT".  The group by will make usernames distinct already, and I don't think you want point scores to be distinct..

[code]$sql = "SELECT uname, SUM(points) AS total GROUP BY uname ORDER BY total DESC, uname ASC LIMIT 10"[/code]

The "uname ASC" ordering is in case of ties.. you need to decide who gets in the top 10 if positions 9-11 have the same score, for example.
Link to comment
Share on other sites

I used your code, btherl, and got
[quote]Can't select from database : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY uname ORDER BY total DESC, uname ASC LIMIT 10' at line 1[/quote]
Link to comment
Share on other sites

Oops.. I just copied and pasted.  You need to say which table to select from too :)

[code]$sql = "SELECT uname, SUM(points) AS total FROM table GROUP BY uname ORDER BY total DESC, uname ASC LIMIT 10"[/code]

where 'table' is the name of your table containing the unames and points.

Algorithmically, that will:

1. Fetch all rows from table
2. Find the sum of points for all matching unames, merging all matching unames into single rows.
3. Order the uname, total (which is sum of points) by total descending, then uname ascending as a tie-breaker.
4. Slice off the first 10 results
5. Return those results to you
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.