I have two tables, one named scores and one named dem0n123.
I am trying to take the AVG of all of the kills in the section of the demon123 table and insert that value into the AverageKills section of scores but only where the Player section of scores equals dem0n123.
I have tried a multitude of different ways and have yet to find anything that works.
* edit *
Better explanation, there are two tables:
-scores
-dem0n123
inside scores there are individual players and there average scores (Average Kills/Deaths/Assists... etc)
inside dem0n123 there are game scores (kills/deaths/assists... etc)
so taking from the dem0n123 table, I need the average of all his kills and then insert that value into the scores table "AverageKills" value
hope that is a better explanation
If I'm guessing what you mean correctly, I think you want this:
UPDATE scores AS s
JOIN (SELECT Player, AVG(kills) AS avkills
FROM dem0n123
GROUP BY Player) AS d
ON s.Player = d.Player
SET s.AverageKills = d.avkills
DEMO
You will need to perform a function on the column. The function you would like is AVG. For example
Select AVG(kills) from scores
Then after that you will want to insert into your other table.
insert into dem0n123(average)
select AVG(kills) from scores
Related
I put together this example to help
http://sqlfiddle.com/#!9/51db24
The idea is I have 3 tables. One is the "root" table, in this case person which has a score attached to it. They then have some category I need to group by person_cat.cat and a one to many field called CS.
I would like to query for the average of the score, the sum of the one to many field person_co.co and group by the category.
SELECT
person_cat.cat,
person.id,
SUM(person_co.co),
AVG(person.cs)
FROM
person
LEFT JOIN person_co USING (id)
LEFT JOIN person_cat USING (id)
GROUP BY cat;
The issue I'm currently having is the average gets thrown off due to the join for the one to many. I can accomplish this with multiple queries, which is ok if that is the answer. However it would be nice to get this as one query.
i am requesting some help for a query to be used on a custom golf website.
what i need is to find the lowest score per player per course. my club has 3 nine hole loops, 27 holes in total, but i want to find the lowest per 9 holes (i.e. course as i am describing it).
i have the following database structure (note, i haven’t put in all rows, only those that are pertinent to the query i am stuggling with).
Golf DB ERP Diagram
a query to get the full set of data would be (note some field names are different - the diagram was trying to better descriptive…):
select * from round r, round_hole rh, player p, course_nine c, course_hole ch
where r.r_id = rh.rh_rid
and p.id = r.r_pid
and c.cn_nine = r.r_nine
and ch.ch_nine = c.cn_nine
and rh.rh_hid = ch.ch_no
a snapshot of the results are:
Full query ouput
however, i then need to filter it as above, into "per player, per course”
i am presuming this is some subquery, join, temp table or “in” type statement, but struggling, particularly as it spans multiple tables.
any help is appreciated
This can be accomplished using some simple aggregation. As long as you are able to properly join all of your tables, you can do this:
SELECT player, course, MIN(score) AS lowestScore
FROM myTables
GROUP BY player, course;
I have two tables. One has a list of baseball players associated with numbers representing their position on the field, and the other table has a list of numbers associated with the names of the field positions.
Using a select statement, how do I select the position number from the players table, and have it show the position name (converting the position number to the name)?
I'm new to MySQL, so please keep it simple and informative. Thank you!
Just join the two tables on the number column.
See MYSQL JOINS HERE: https://dev.mysql.com/doc/refman/5.0/en/join.html
Here is an example, since I don't know your actual schema.
SELECT pt.player_name as PLAYERNAME, fp.position_name as POSITION
FROM playersTable AS pt
INNER JOIN fieldPositions AS fp ON fp.position_number = pt.position_number
You can try like this
SELECT pf.position_name as position, p.position_number as playerName
FROM field_positions as pf, player as p
WHERE fp.position_number = p.position_number
I have two tables.
Users:
int player_id
varchar player_name
Games:
int game_id
int player_id1
int player_id2
I want to make a query that takes in a player id as a parameter, and returns info on each game, along with the player's name. So far, what I have is the following:
SELECT
game_id,
player_id1,
player_id2,
from GAMES, GAME_STATES
where player_id1=#playerid or player_id2=#playerid
The part I'm stuck at is a simple way to have it return the names of players along with the player ids. The returning query would have 5 columns, one of the game id, two for each player id, and two for each of their names.
One solution I thought of is:
SELECT
game_id,
player_id1,
(select player_name from USERS where player_id=player_id1) as player_name1, player_id2,
(select player_name from USERS where player_id=player_id2) as player_name2,
from GAMES, GAME_STATES
where player_id1=#playerid or player_id2=#playerid
However, this seems like a lot of extra work on the database since there would be 2 more queries per row returned. If I have to do that, I'm wondering if making requests for names as a second query on the client side is a better option? Then the client could create a list of unique ids, and do one query for all of them. I'm not too worried about latency since the client and server are in the same data center.
Thank you for your help.
SELECT game_id, u1.player_name, u2.player_name FROM games AS game INNER JOIN users AS u1 ON y1.playerid = game.player_id1 INNER JOIN users AS u2 ON u2.playedid = game.player_id2 WHERE player_id1 = #playerid OR player_id2 = #playerid
Should do the trick
Hello all and thanks in advance
I have the tables accounts, votes and contests
A vote consists of an author ID, a winner ID, and a contest ID, so as to stop people voting twice
Id like to show for any given account, how many times theyve won a contest, how many times theyve come second and how many times theyve come third
Whats the fastest (execution time) way to do this? (Im using MySQL)
After using MySQL for a long time I'm coming to the conclusion that virtually any use of GROUP BY is really bad for performance, so here's a solution with a couple of temporary tables.
CREATE TEMPORARY TABLE VoteCounts (
accountid INT,
contestid INT,
votecount INT DEFAULT 0
);
INSERT INTO VoteCounts (accountid, contestid)
SELECT DISTINCT v2.accountid, v2.contestid
FROM votes v1 JOIN votes v2 USING (contestid)
WHERE v1.accountid = ?; -- the given account
Make sure you have an index on votes(accountid, contestid).
Now you have a table of every contest that your given user was in, with all the other accounts who were in the same contests.
UPDATE Votes AS v JOIN VoteCounts AS vc USING (accountid, contestid)
SET vc.votecount = vc.votecount+1;
Now you have the count of votes for each account in each contest.
CREATE TEMPORARY TABLE Placings (
accountid INT,
contestid INT,
placing INT
);
SET #prevcontest := 0;
SET #placing := 0;
INSERT INTO Placings (accountid, placing, contestid)
SELECT accountid,
IF(contestid=#prevcontest, #placing:=#placing+1, #placing:=1) AS placing,
#prevcontest:=contestid AS contestid
FROM VoteCounts
ORDER BY contestid, votecount DESC;
Now you have a table with each account paired with their respective placing in each contest. It's easy to get the count for a given placing:
SELECT accountid, COUNT(*) AS count_first_place
FROM Placings
WHERE accountid = ? AND placing = 1;
And you can use a MySQL trick to do all three in one query. A boolean expression always returns an integer value 0 or 1 in MySQL, so you can use SUM() to count up the 1's.
SELECT accountid,
SUM(placing=1) AS count_first_place,
SUM(placing=2) AS count_second_place,
SUM(placing=3) AS count_third_place
FROM Placings
WHERE accountid = ?; -- the given account
Re your comment:
Yes, it's a complex task no matter what to go from the normalized data you have to the results you want. You want it aggregated (summed), ranked, and aggregated (counted) again. That's a heap of work! :-)
Also, a single query is not always the fastest way to do a given task. It's a common misconception among programmers that shorter code is implicitly faster code.
Note I have not tested this so your mileage may vary.
Re your question about the UPDATE:
It's a tricky way of getting the COUNT() of votes per account without using GROUP BY. I've added table aliases v and vc so it may be more clear now. In the votes table, there are N rows for a given account/contest. In the votescount table, there's one row per account/contest. When I join, the UPDATE is evaluated against the N rows, so if I add 1 for each of those N rows, I get the count of N stored in votescount in the row corresponding to each respective account/contest.
If I'm interpreting things correctly, to stop people voting twice I think you only need a unique index on the votes table by author (account?) ID and contestID. It won't prevent people from having multiple accounts and voting twice but it will prevent anyone from casting a vote in a contest twice from the same account. To prevent fraud (sock puppet accounts) you'd need to examine voting patterns and detect when an account votes for another account more often then statistically likely. Unless you have a lot of contests that might actually be hard.