How to find the smallest difference between two columns in Mysql? - mysql

I have a table in my database which contains data for recording games between people. I wanted to have a query that would return the game which was closest between two players, ie, the smallest difference between the two scores recorded, regardless of who won. I have started with something like this as a query, but I can't quite get what I want.
SELECT recorder_score, opponent_score
from games
where recorder_id = $recorder_id
order by (recorder_score - opponent_score)
limit 1
The above obviously would only return the closest game that the person submitting the game has won, but as I mentioned, I want the closest game regardless of who wins. What will be the best way of doing this?

Use ABS in Order by to get the closest game between two players
SELECT recorder_score, opponent_score
from games
where recorder_id = $recorder_id
order by ABS(recorder_score - opponent_score)
limit 1

Related

MySQL query for an MLB database

There are 5 tables: mlb_batting, mlb_manager, mlb_master, mlb_pitching, mlb_team.
Find the top 10 (highest) “strike outs per walk” statistic for all pitches with at least 1 walk that played in at least 25 games. You should display their first name, last name, and K/BB statistic. K/BB is computed by dividing the number of strike outs by the number of walks (“base on balls”). You will need to use “limit” in MySQL (not talked about in class or notes – you will have to search how to do it). I would like this query done 2 different ways. One that only looks at the 25 games and 1 walk on a per stint basis. That is, if they played for two different teams (two different stints) then you would count those separate. And the other query should combine all the stints they had. That is, if they played for two different teams you would add up their games and walks.
My solution is:
SELECT NAME_FIRST, NAME_LAST, SUM(strikeouts) / SUM(walks) AS KS_PER_BB
FROM mlb_master
JOIN mlb_pitching
ON mlb_master.player_id = mlb_pitching.player_id
WHERE walks >= 1 AND games >= 25
GROUP BY name_first, name_last, mlb_pitching.stint
ORDER BY KS_PER_BB DESC
LIMIT 10;
I am wondering if this solution is better for the first way my professor wants it done or the second way, if any.
This solution is appropriate for the first query because by having GROUP BY stint, each stint is considered different for each player.
For the second way, could I remove the stint column from the GROUP BY clause so that it groups the records for a particular player together, regardless of the different stints they played for?
Would this result in the sum of all their walks and strikeouts from all their stints being used to calculate the KS_PER_BB statistic, giving you the combined total for each player?

GROUP BY one variable but display multiple SQL

I'm trying to GROUP BY one categorical variable but also show another corresponding variable in my output in SQL. Here is what the table looks like:Original Data Table
There are three main variables I'm working with here: Game, Platform and Week. Week variable states the position of the game under the global charts, so 1 would mean its the number 1 game. I want to find the game with the most weeks inside the top 10 BY platform, so I'm trying to get my table to look like this:
Platform | Game | Most_weeks_top10
Right now, I tried the following steps:
SELECT platform, game, COUNT(*) AS total
FROM global_weekly_charts_2013_2014
WHERE week <= 10
GROUP BY game, platform;
Which returns this:
Table Grouped By Platform AND Game
However, I only want the game, platform and total weeks of the game with max number of weeks in top 10. I tried
SELECT game, platform, Max(total) OVER (PARTITION BY platform)
from the derived table but did not get the desired output. I feel like the solution is right there and not that difficult but I can't seem to get the answer.
please add “ORDER BY total DESC LIMIT 10”.try again

Query to get number of records where 0, using 2 tables

Table Structure
Table structure brief
We have 2 tables Weight and Weight_Sub, they are both identical. Our users weigh-in every 2 days and every team have a captain. So after each weigh-in team captain adds the weight according to the date in the Weight_Sub table and for that we add records in Weight table with weight 0. Once the team player accepts that score we delete them from Weight_Sub and update scores in main table i.e. Weight. E.g. If a player on 01/03/2015 was weighed 98KG we'll add there record in Weight_Sub= 98 and in Weight= 0. Once player will accept that weight then we will delete that row from Weight_Sub table and update weight in Weight table from 0 to 98.
I hope it makes sense.
Problem
We want to make a check when player accepts the score that did he missed any weigh-in. Like in the above table for 03/03/2015 in both tables the weight is 0,0 that means player missed a weigh-in. We want to get those missed weigh-in dates. The thing is we only want missed weigh-in dates which were missed after accepting the last weight, in this case on 07/03/2015.
So if you see the above image, player missed weigh-in on the 03/03/2015 but on 07/03/2015 he accepted the weight as 100KG. After that he missed 3 consecutive weighin's and then accepted on the 15/03/2015. So the query should return 3 dates i.e. 09/03/2015, 11/03/2015, 13/03/2015.
I know it can be done using PHP by running the loop and breaking when I find the accpted scores and get dates for missed weighin's but I'm looking for something for more efficient if there is.
This query will return missed records before a given date.
select w1.ID, w1.Date, w1.Weight
from `WeightTable` w1
where w1.Date < '07/03/2015' and w1.Weight = 0

Count, max, and multiple sub querys SQL

I'm currently working on a league systeme for my sport team. A ladder, as seen as in some video games.
It's a mobile web site, allowing coaches to create games, and monitor players performances.
I have games automatically balanced, taking into accounts player's experiences and points, then, i give bonus points to the all the players of the winner team, and remove points from the losers.
I have a relatively simple database. 3 tables.
User : id - name
Games : id - ETA - cration_date
game_joueur: id- id_game - id_joueur - team - result - bonus
game_joueur beeing an assoc table, in wich i register for each new game players id, the team he has been seeded on, and afterwards, update the bonus field with the points earned and the result field with an integer (1 = lose, 2= win)
That way i can sum the bonus on my players stat and get the total points.
You can have a better look at the table here :
http://sqlfiddle.com/#!2/d3e06/2
What i'm tryng to acomplish is for each player's stat page, retrieve from the database the name of his most succesfull partner( the guy wich whom he won the most games), and also his worst ally , the men he lost the most match with.
This is what i do on my user stat page :
SELECT
(SELECT COUNT(lad_game_joueur.result) FROM lad_game_joueur WHERE result = 1 AND lad_game_joueur.id_joueur = lad_user.id) as lose,
(SELECT SUM(lad_game_joueur.bonus) FROM lad_game_joueur WHERE lad_game_joueur.id_joueur = lad_user.id) as points,
lad_user.id as id ,
(SELECT COUNT(lad_game_joueur.result) FROM lad_game_joueur WHERE lad_game_joueur.id_joueur = lad_user.id AND result =2) as win,
lad_user.name
FROM lad_user,lad_game_joueur
WHERE lad_game_joueur.id_joueur = lad_user.id AND lad_user.id
='.$id_joueur.'
GROUP BY lad_user.id
ORDER BY puntos DESC
I'm sure this is not the best way to do it, but it works :) ( i'm no sql specialist)
How can i tune this query to also retrive the informations i'm looking for?
I wont mind doing another query.
Thanks a lot in advance!
Ben
Ok i finealy found a way.
Here's what i did :
SELECT
SUM(result)as result_sum, sum(Bonus) as bonus_sum, id_joueur
from lad_game_joueur
where result= 2
and id_game in
(SELECT lad_game_joueur.id_game from lad_game_joueur,lad_game where id_joueur=2
AND result= 2 and lad_game_joueur.id_game=lad_game.id)
group by id_joueur
order by result_sum DESC, bonus_sum desc
As you see, the sum of result would give me 4 if i won two games with the person, but i just divide by 2 on php and voilà :)

How to retrieve data for a statistics page using MYSQL Queries

I have a table named tblMatches containing match results of a sporting event. The score for the winning player is always stored in the P1_Score field, the P2_Score tracks the losing player score.
There are 4 fields within the table that I need to reference.
(P1_ID), (P1_Score), (P2_ID), (P2_Score)
I am passing through an ID_value. I have managed to calculated the total match wins vs total match losses using a mysql_num_rows function.
I am now trying to calculate the total number of games won vs the total number of games lost. I need to somehow determine the sum of the scores.
What about
SELECT (SELECT SUM(P1_Score) FROM tbl WHERE P1_ID = $id) won,
(SELECT SUM(P2_Score) FROM tbl WHERE P2_ID = $id) lost
replace $id with particular player's id and you'll get how much games he won and lost accordingly