0

I have a website that is going to rank users based on points they have earned for doing certain activities. This is my database structure:

pointsentered
1. ID
2. Email
3. Code

pointvalues
1. ID
2. Code
3. Title
4. Value

I have a file that connects to pointsentered and submits the user-selected to it. The "Code" field corresponds to the pointvalues.

In pointvalues, the "Title" corresponds to the name of the event they completed. The code is a simple alpha-numeric code that corresponds to that specific event and the value it holds. "Value" is fairly self explanatory, as it holds the point value that the event relates to.

Essentially, I want to have a script that
1. Ranks all of the users (who have different emails) based on entered points in pointsentered (but grabs points from pointvalues based on the "Code") 2. Shows how many points the user has attained. It relates to the user's email.

Obviously, I am not looking for someone to do this for me, I am just looking for some guidance on how to get started, as I am lost.

Thanks in advance.

2
  • Please share with us what you have tried. Commented Sep 15, 2014 at 21:49
  • @JayBlanchard I have not yet attempted to do this because I am not sure of where to start, what functions to use etc. I am not looking for someone to do it for me, just somewhere to start. Commented Sep 15, 2014 at 21:58

1 Answer 1

0

This should be fairly easy to achieve with a simple query along the lines of:

SELECT pe.Email, SUM(pv.Value) as 'total_points' FROM pointsentered pe
JOIN pointvalues pv ON pe.Code = pv.Code
GROUP BY pe.Email
ORDER BY total_points DESC

What's going here is you have two tables pointsentered and pointvalues. The pointsentered table contains the Email (pe.Email) of the person who was awarded points. Also in that table is a Code (pe.Code) which corresponds to a row in the pointvalues table. So we can query against the pointsentered and JOIN the pointvalues table to get a list of all awarded points, the email of the person who was awarded, and the number of points awarded. We can also apply an aggregate function to the point values (SUM) and group by the persons Email, yielding a list of all unique Emails along with the total number of points awarded to that Email, starting with the highest number of total points (DESC).

5
  • Thanks for the swift response... Could you just clarify the different parts to this? For example, the "pe.email" and "pv.Value" etc. Commented Sep 15, 2014 at 21:49
  • This helps a lot, thanks. So when you have "pe.Email", and "pv.Value", etc. I can just write "Email" or "Value?" Commented Sep 15, 2014 at 22:04
  • you can as long as the column names are not ambiguous, when joining multiple tables its nice to give the table names aliases so you can easily and explicitly declare which column and which table you are referencing
    – A.O.
    Commented Sep 15, 2014 at 22:05
  • I'm sorry, but I'm still confused. Can you explain once more how to use them? I now understand that pe = pointsentered and pv = pointvalues Commented Sep 15, 2014 at 22:09
  • I suggest doing some research on MySQL queries, specifically JOINS, aliases, and aggregates....dev.mysql.com/doc
    – A.O.
    Commented Sep 15, 2014 at 22:15

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.