Jump to content

PHP/MYSQL Query to block duplicate entries


php2010

Recommended Posts

The only 2 tables relevant to what I want to do are "User" and "Scores." Basically this database holds high scores for a project I've been working on. I only want to display 10 scores, all from different users. No 2 or more scores from the same user. This is my db structure within phpmyadmin:

 

database -> table -> X / User / Score / X / X / X / X / X / X / X / X

 

If possible I'd like to run a query where it checks for duplicate entries by the same user and only show the highest score. The current query I'm running is below:

 

$result = mysql_query("SELECT * FROM table ORDER BY Score DESC LIMIT 0, 10");

 

Is this possible? I can only seem to find information regarding the INSERT IGNORE clause and that obviously won't help me as the users and scores are already in the database. Thanks.

Link to comment
Share on other sites

I don't know if I explained it correctly. Will the above queries work like this?

 

This is what I mean:

 

user1 | score 1

user2 | score 2

user3 | score 3

user4 | score 4

user5 | score 5

user6 | score 6

user7 | score 7

user8 | score 8

user9 | score 9

user10 | score 10

 

Basically a top ten that excludes multiple scores from the same user. Say user1 submitted 5 scores, I don't want to display all 5 I JUST want to display the highest score from that user and then go on to whoever is second place and so on. Is that possible?

 

Sorry if I explained it wrong I just want to make sure we understand each other before I go tinkering with things. :P Thanks guys!

 

 

Edit: Also I would like to mention that I do need the other sections in the table. Like the 'X / User / Score / X / X / X / X / X / X / X / X' All those X's represent other data that I also need that's echoed into an html table.

Link to comment
Share on other sites

Yes it is possible, and that query will do it.

 

If you need the other data items as well then things can get tricky.  It it possible for the same user to have two entries with the same score?  The answer to that determines how the query will look.  It's simpler if they cannot have two entries with the same score, as then (user_id, score) is a unique identifier for the row you want to display.  Otherwise you'll need a way to choose between two equally high scoring rows.

Link to comment
Share on other sites

There's no reason why anyone would enter the same score twice via the submit form and even if they do, I would go in and delete it manually if I had to. I will try that query you posted in a little bit. I'm in the process of finishing up some stuff for work right now and don't have the time to do it right at this moment. Maybe in an hour or so. :shy:

 

I will report back how it works after I enter some fake scores to test it later. Thanks. :D

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.