Find total number of times a number appears in two columns - php

I have a MySQL database where I add soccer games shown on TV. Each team is represented as an number. I can't really figure out how I can make a query to list how many times a team has been shown on TV, no matter if they played at home or away.
I'm trying to make a top 20 list of teams thats been shown on TV. The two columns I have team id in are called "hjemmehold" and "udehold" (it's danish :)).
Anyone can help me here?

SELECT Team, Count(*)
FROM (select Away as Team from Games union all select Home as Team from Games) t
GROUP BY Team
ORDER BY Count(*) Desc
LIMIT 20

SELECT SUM(Away)+SUM(Home) AS NumGames
FROM Games
WHERE Team=#Team
Obviously this is pseudocode, but put the proper tables/fields/params in and you should be good to go.
For all 20 teams:
SELECT TOP 20 Team, SUM(Away)+SUM(Home) AS NumGames
FROM Games
ORDER BY SUM(Away)+SUM(Home) desc

t.his should be able to give you the top 20 teams that have played. This will also allow you to find which teams have not yet played a game (if you remove the top 20 part).
SELECT TOP 20
team_id,
(SELECT COUNT(1) FROM GAMES WHERE HOME = t.team_id) + (SELECT COUNT(1) FROM GAMES WHERE AWAY = team_id) AS team_count
FROM TEAMS T
ORDER BY team_count DESC

Related

MySQL Rank, Cannot get it working properly

I know this has been covered somewhat but it hasn't helped my situation, rather there's been discrepancies in the rank. Wondering if anyone would be able to help!
So i have a game, the database tables are:
users, maps, nicknames, user_game_scores
Im developing a leader board and easily able to get the information ordered by score. fantastic.
But i want to rank this so that i can pull a specific users scores and the rank be relevant to all scores. eg:
GLOBAL SCORES
user info - Score - (rank)1
user info - Score - (rank)2
user info - Score - (rank)3
etc.
Whereas USER SCORES are more likely to be:
user info - Score - (rank)82
user info - Score - (rank)94
user info - Score - (rank)115
etc.
I imagine the implementation to be this:
SELECT users.first_name, users.surname, player_nicknames.nickname, maps.map_name, user_game_scores.score,
FIND_IN_SET( score, ( SELECT GROUP_CONCAT( score ORDER BY score DESC ) FROM user_game_scores ) ) AS rank
FROM `user_game_scores`
INNER JOIN users ON user_game_scores.user_id = users.user_id
INNER JOIN maps ON user_game_scores.map_id = maps.map_id
INNER JOIN player_nicknames ON user_game_scores.user_id = player_nicknames.user_id
WHERE user_game_scores.deleted is null
AND users.deleted is null
AND player_nicknames.deleted is null
ORDER BY user_game_scores.score DESC
But it returns this: (click here) - names etc have been removed from the image as it may not be appropriate to display
As you can see the Rank tends to miss a number or two (number 2 and 23). i understand that something like rank 24 will group and continue (which i prefer to happen in that instance) but i dont understand why some of the rank is missing and really dont want to post process this functionality.
Sorry this is long but i thought id provide as much information as i can. Thanks in advance!
It's probably because your SELECT GROUP_CONCAT subquery doesn't filter "deleted" (deleted is null) entries. – Paul Spiegel 9 hours ago

what is the best practice leaderboard with php, mysql, memcached?

Recently I have developed mobile game (LAMP + memcached).
The game has player's score table. The table has member_id, name, score column.
I want to show leaderboard (global rank) to our user.
Just query SELECT * FROM score ORDER BY score DESC, and show the resultset.
But I don't think it is good way. If users reach 20 million, this query seem to be terrible. What is the best practice in this case? Could you give me some advice?
And how can I check specific user's rank? I want to show each user rank where they are.
Well, you don't want to display millions of users on the leader board do you?
SELECT * FROM score ORDER BY score DESC LIMIT 10
Would select top 10
Further, if you want to display a users rank on for instance a profile page this should do:
SELECT COUNT(*) + 1 AS rank FROM score WHERE score > (SELECT score FROM score WHERE member_id = :member_id);
You simply add the current users member_id to the last query and it will count all rows ahead of him and add 1 to that.

Highscores (top scores) rank calculation & update query?

I have a high scoring (top scores) system, which is calculating positions by players's eperience.
But now I need to use the player's rank in other places just the web, maybe more places in the web too like personal
high scores, and it will show the player's rank in that skill.
Therefore just looping & playing with the loop cycle like rank++ won't really work, cause I need to save that rank for
other places.
What I could do is loop through all players and then send a query to update that player's rank, but what if i have 1000 players? or more?
that means 1000 queries per load.
I have thought if there could be a SQL query I can use to do the same action, in one or two queries.
How can I do this? I calculate ranks by ordering by player's eperience, so my table structure looks like this:
Tables:
Players
id (auto_increment) integer(255)
displayname varchar(255) unique
rank integer(255) default null
experience bigint(255)
This should give you the rank for user with id = 1. If you want every player, just remove the WHERE clause:
SELECT a.id, a.displayname, a.rank, a.experience
FROM (
SELECT id, displayname, #r:=#r+1 AS rank, experience
FROM players, (SELECT #rank:=0) tmp
ORDER BY experience DESC) a
WHERE a.id = 1
I wouldn't have rank in the players table directly, since this would mean that you would have to recalculate it every time a user changes experience. You could do this query anytime you want to get the rank for a player or for a leaderboard.
If you still want to update it, You can do an INNER JOIN with this query to UPDATE the original table with the rank from this query.

This query (SELECT JOIN SUM ON) worked well before on another project however now it doesn't

The below query worked fine on another project (SQL), however now it seems to give no results and i do not get why. Have search allready for hours on the web for solutions.
I have 2 tables, one GOALS (id_goals, season, player, goalsscored, assists, team) and another PLAYERS (id_player, name, and some more fields not important for this matter). id_player from PLAYERS is used in player field in GOALS. Now i want to display the name of the players and the total goals they have scored as well as the total assists they have made in a certain season for a certain team and this is the query I am using (but does not work anymore):
mysql_select_db($database_check_mag, $check_mag);
$query_getPosts = "SELECT id, tot_goals, player_name, tot_assists FROM
(SELECT g.player id, SUM(g.goalsscored) tot_goals, SUM(g.assists) tot_assists,
g.season, s.name player_name, s.id_player FROM goals g JOIN players s ON
g.player = s.id_player WHERE g.season = 20132 AND g.team = 39
GROUP BY g.player) sums GROUP BY id ORDER BY tot_goals DESC";
Can somebody help me and tell me what I do wrong?

Complicated MySQL query?

I have two tables as follows:
I have a RatingsTable that contains a ratingname and a bit whether it is a positive or negative rating:
RatingsTable
----------------------
ratingname ispositive
----------------------
Good 1
Bad 0
Fun 1
Boring 0
And I have a FeedbackTable that contains feedback on things: the person rating, the rating and the thing rated. The feedback can be determined if it's a positive or negative rating based on the RatingsTable.
FeedbackTable
---------------------------------
username thing ratingname
---------------------------------
Jim Chicken Good
Jim Steak Bad
Ted Waterskiing Fun
Ted Hiking Fun
Nancy Hiking Boring
I am trying to write an efficient MySQL query for the following:
On a page, I want to display the the top 'things' that have the highest proportion of positive ratings. I want to be sure that the items from the feedback table are unique...meaning, that if Jim has rated Chicken Good 20 times...it should only be counted once. At some point I will want to require a minimum number of ratings (at least 10) to be counted for this page as well. I'll want to to do the same for highest proportional negative ratings, but I am sure I can tweak the one for positive accordingly.
To get the "things" in order of proportion of good ratings you can use this query:
SELECT thing, SUM(ispositive) / COUNT(*) AS proportion_positive
FROM (SELECT DISTINCT username, thing, ratingname FROM FeedbackTable) T1
JOIN RatingsTable T2
ON T1.ratingname = T2.ratingname
GROUP BY thing
ORDER BY proportion_positive DESC
For your example data it returns this:
thing proportion_positive
Chicken 1.0000
Waterskiing 1.0000
Hiking 0.5000
Steak 0.0000
To require at least 10 votes before displaying a thing in the results add this line after the GROUP BY:
HAVING COUNT(*) >= 10
To get the proportion of negative ratings change SUM(ispositive) to SUM(NOT ispositive).
Note: it might be better to add a unique constraint to your voting table instead of selecting only the disctinct values.
SELECT *
FROM `feedback`
LEFT JOIN `ratings` ON `feedback`.`rating` = `rating`.`label`
ORDER BY `rating`.`value` DESC
GROUP BY `feedback`.`username`
LIMIT 10
The summary: join the ratings to your feedback table, but group by the username so you only get one username per result.

Categories