I have two tables described below. What I need is a single query that will tell me the players whose score hasn’t changed in a given number of days.
CREATE TABLE players (
pid INT(50),
name VARCHAR(255),
updatedAt DATETIME
);
CREATE TABLE pl_scores (
pid INT(50),
score INT(255),
updatedAt DATETIME
);
The players table keeps a master list of all players along with other non-changing data not shown here for the sake of brevity. The pl_scores table keeps a running history of score changes in order to track growth and other values that might change. A new record is inserted into pl_scores for each player every 6 hours.
What I would like to get is the pid of the players that haven’t had a change of score in a certain number of days, but I am unsure of how to group that in order to get the correct values.
Example dataset
(only shows last score of each day which is only one needing to be compared really)
+------+------+-------+------+---------+---------------------+
| pid | aid | score | rank | cityCnt | updatedAt |
+------+------+-------+------+---------+---------------------+
| 1660 | 0 | 801 | 2111 | 1 | 2012-06-20 22:14:11 |
| 1660 | 0 | 801 | 2250 | 1 | 2012-06-21 22:15:45 |
| 1660 | 0 | 801 | 2387 | 1 | 2012-06-22 22:17:06 |
| 1660 | 0 | 801 | 2547 | 1 | 2012-06-23 22:17:09 |
| 1660 | 0 | 801 | 2702 | 1 | 2012-06-24 22:19:50 |
| 1660 | 0 | 801 | 2836 | 1 | 2012-06-25 22:21:07 |
| 1660 | 0 | 801 | 2956 | 1 | 2012-06-26 21:42:44 |
+------+------+-------+------+---------+---------------------+
EDIT
The answer found below worked perfectly, but now I’d like to take it a step further and limit the results by a hardcoded value found in a 3rd table. Here is the working SQL statement
SELECT a.pid, c.aid, b.name AS pName, c.name AS aName, a.score FROM pl_scores AS a
JOIN players AS b ON a.pid = b.pid
JOIN alliances AS c ON b.aid = c.aid
WHERE a.updatedAt >= CURRENT_DATE() - INTERVAL 3 DAY GROUP BY a.pid HAVING MIN(a.score) = MAX(a.score);
Each player has multiple cities throughout the world. I’d like to limit the results by players with cities found on a given continent. For example, I want to find every player that hasn’t changed their score in the last 3 days on continent 34. The cities table looks like this:
CREATE TABLE cities (
cid INT(50),
pid INT(50),
name VARCHAR(255),
cont INT(10),
updatedAt DATETIME
);
You could get
pids of players with their scores unchanged like this:Now you can use those
pids to get full (or just more) information about the corresponding players, i.e. like this: