Get entry with max value in MySQL - mysql

I've got a MySQL database with lots of entris of highscores for a game. I would like to get the "personal best" entry with the max value of score.
I found a solution that I thought worked, until I got more names in my database, then it returnes completely different results.
My code so far:
SELECT name, score, date, version, mode, custom
FROM highscore
WHERE score =
(SELECT MAX(score)
FROM highscore
WHERE name = 'jonte' && gamename = 'game1')
For a lot of values, this actually returns the correct value as such:
JONTE 240 2014-04-28 02:52:33 1 0 2053
It worked fine with a few hundred entries, some with different names. But when I added new entries and swapped name to 'gabbes', for the new names I instead get a list of multiple entries. I don't see the logic here as the entries in the database seem quite identical with some differences in data.
JONTE 176 2014-04-28 11:03:46 1 0 63
GABBES 176 2014-04-28 11:09:12 1 0 3087
The above has two entires, but sometimes it may also return 10-20 entries in a row too.
Any help?

If you want the high score for each person (i.e. personal best) you can do this...
SELECT name, max(score)
FROM highscore
WHERE gamename = 'game1'
GROUP BY name
Alternatively, you can do this...
SELECT name, score, date, version, mode, custom
FROM highscore h1
WHERE score =
(SELECT MAX(score)
FROM highscore h2
WHERE name = h1.name && gamename = 'game1')
NOTE: In your SQL, your subclause is missing the name = h1.name predicate.
Note however, that this second option will give multiple rows for the same person if they recorded the same high score multiple times.

The multiple entries are returned because multiple entries have the same high score. You can add LIMIT 1 to get only a single entry. You can choose which entry to return with the ORDER BY clause.

Related

Remove list of repetitions in a list SQL

I have a table for a game where more people play the game it will repeat each other.. I can remove duplicates no problem but this one is different because I want to remove the others while keeping the highest score is it possible to do it?
for example
Name Level Score
Green 99 797,000
Green 99 819,000
Green 99 970,000
Green 99 890,000
I want to keep row 3 and remove the others.
In the example you show, you can get the top row this way:
SELECT * FROM `thistable` ORDER BY score DESC LIMIT 1
But I assume you have more than one Name in the table. If you want the highest score for a specific name:
SELECT * FROM `thistable` WHERE Name = 'Green' ORDER BY Score DESC LIMIT 1
If you want results for multiple names, but the row with the highest score for each one, it's a bit more complex:
SELECT * FROM `thistable`
JOIN (SELECT Name, MAX(Score) AS Score FROM `thistable` GROUP BY Name) AS x
USING (Name, Score);
This type of problem is commonly tagged greatest-n-per-group and it's a frequent question.
PS: Don't take the downvotes seriously. Stack Overflow has become a pretty nasty place. There will always be someone who thinks you didn't ask a valid question, or didn't ask it "correctly."

SELECT the oldest of the most recent lines

I have a table storing the scores (with the date) of players they did at each game.
Example:
john 154 10/02/2014
mat 178 09/02/2014
eric 270 08/02/2014
mat 410 07/02/2014
john 155 06/02/2014
In this example I want "eric 270 08/02/2014" because thins is the oldest of the most recents.
Which request must I do to retrieve that ?
As I understand it, you request the oldest entry among the set containing the most recent one of each user.
In such a case, you can deal with your problem using a subquery given the last date for each user, then used in the main query to select and sort only the most recent entry of each user.
SELECT scores.*
FROM scores
INNER JOIN
(
SELECT max(date) last, name
FROM scores
GROUP BY name
) last_temp_table
ON scores.name = last_temp_table.name
AND scores.date = last_temp_table.last
ORDER BY scores.date ASC LIMIT 1;
More info in different SO threads such as MySQL order by before group by
The question as worded doesn't make much sense unless you define most recent.
If I assume that you have some criteria like: "Give the oldest event that happened within the last 3 days" then that is a simple matter of ordering and limiting across a date range.
select * from events where ts >= CURDATE() - 3
order by ts asc
limit 1

Relational Database Logic

I'm fairly new to php / mysql programming and I'm having a hard time figuring out the logic for a relational database that I'm trying to build. Here's the problem:
I have different leaders who will be in charge of a store anytime between 9am and 9pm.
A customer who has visited the store can rate their experience on a scale of 1 to 5.
I'm building a site that will allow me to store the shifts that a leader worked as seen below.
When I hit submit, the site would take the data leaderName:"George", shiftTimeArray: 11am, 1pm, 6pm (from the example in the picture) and the shiftDate and send them to an SQL database.
Later, I want to be able to get the average score for a person by sending a query to mysql, retrieving all of the scores that that leader received and averaging them together. I know the code to build the forms and to perform the search. However, I'm having a hard time coming up with the logic for the tables that will relate the data. Currently, I have a mysql table called responses that contains the following fields,
leader_id
shift_date // contains the date that the leader worked
shift_time // contains the time that the leader worked
visit_date // contains the date that the survey/score was given
visit_time // contains the time that the survey/score was given
score // contains the actual score of the survey (1-5)
I enter the shifts that the leader works at the beginning of the week and then enter the survey scores in as they come in during the week.
So Here's the Question: What mysql tables and fields should I create to relate this data so that I can query a leader's name and get the average score from all of their surveys?
You want tables like:
Leader (leader_id, name, etc)
Shift (leader_id, shift_date, shift_time)
SurveyResult (visit_date, visit_time, score)
Note: omitted the surrogate primary keys for Shift and SurveyResult that I would probably include.
To query you join shifts and surveys group on leader and taking the average then jon that back to leader for a name.
The query might be something like (but I haven;t actually built it in MySQL to verify syntax)
SELECT name
,AverageScore
FROM Leader a
INNER JOIN (
SELECT leader_id
, AVG(score) AverageScore
FROM Shift
INNER JOIN
SurveyResult ON shift_date = visit_date
AND shift_time = visit_time --depends on how you are recording time what this really needs to be
GROUP BY leader ID
) b ON a.leader_id = b.leader_id
I would do the following structure:
leaders
id
name
leaders_timetabke (can be multiple per leader)
id,
leader_id
shift_datetime (I assume it stores date and hour here, minutes and seconds are always 0
survey_scores
id,
visit_datetime
score
SELECT l.id, l.name, AVG(s.score) FROM leaders l
INNER JOIN leaders_timetable lt ON lt.leader_id = l.id
INNER JOIN survey_scores s ON lt.shift_datetime=DATE_FORMAT('Y-m-d H:00:00', s.visit_datetime)
GROUP BY l.id
DATE_FORMAT here helps to cut hours and minutes from visit_datetime so that it could be matched against shift_datetime. This is MYSQL function, so if you use something else you'll need to use different function
Say you have a 'leader' who has 5 survey rows with scores 1, 2, 3, 4 and 5.
if you select all surveys from this leader, sum the survey scores and divide them by 5 (the total amount of surveys that this leader has). You will have the average, in this case 3.
(1 + 2 + 3 + 4 + 5) / 5 = 3
You wouldn't need to create any more tables or fields, you have what you need.

Complex MySQL COUNT query

Evening folks,
I have a complex MySQL COUNT query I am trying to perform and am looking for the best way to do it.
In our system, we have References. Each Reference can have many (or no) Income Sources, each of which can be validated or not (status). We have a Reference table and an Income table - each row in the Income table points back to Reference with reference_id
On our 'Awaiting' page (the screen that shows each Income that is yet to be validated), we show it grouped by Reference. So you may, for example, see Mr John Smith has 3 Income Sources.
We want it to show something like "2 of 3 Validated" beside each row
My problem is writing the query that figures this out!
What I have been trying to do is this, using a combination of PHP and MySQL to bridge the gap where SQL (or my knowledge) falls short:
First, select a COUNT of the number of incomes associated with each reference:
SELECT `reference_id`, COUNT(status) AS status_count
FROM (`income`)
WHERE `income`.`status` = 0
GROUP BY `reference_id`
Next, having used PHP to generate a WHERE IN clause, proceed to COUNT the number of confirmed references from these:
SELECT `reference_id`, COUNT(status) AS status_count
FROM (`income`)
WHERE `reference_id` IN ('8469', '78969', '126613', ..... etc
AND status = 1
GROUP BY `reference_id`
However this doesn't work. It returns 0 rows.
Any way to achieve what I'm after?
Thanks!
In MySQL, you can SUM() on a boolean expression to get a count of the rows where that expression is true. You can do this because MySQL treats true as the integer 1 and false as the integer 0.
SELECT `reference_id`,
SUM(`status` = 1) AS `validated_count`,
COUNT(*) AS `total_count`
FROM `income`
GROUP BY `reference_id`

mySQL count occurances of a string

I have this query...
SELECT SUM(brownlow_votes) AS votes,
player_id,
player_name,
player_team,
COUNT(*) AS vote_count
FROM afl_brownlow_phantom, afl_playerstats
WHERE player_id=brownlow_player
AND brownlow_match=player_match
GROUP BY player_id
ORDER BY votes DESC LIMIT 50
So "votes" becomes the number of votes a player has, "vote_count" becomes the number of times (matches in which) a player has been voted for. This works fine.
However, I have another column called "brownlow_lock" which is either blank, or 'Y'. How do I get the number of occurances of 'Y'? I know I could solve this changing it to 0 or 1 and just doing a SUM() but I don't want to have to go and edit the tons of pages that are inserting data.
If I have understood you correctly you just need to add
COUNT(CASE WHEN brownlow_lock='Y' THEN 1 END) AS Cnt
to your query
Try using the IF control flow function
SELECT SUM(IF(brownlow_lock='Y',1,0)) lock_count ...