Determine winner of games and joining with table of names - mysql

I have two tables, one called players, one called matches.
The players table contains 2 columns: id, name
The matches table contains 5 columns: player1, player2, goalsforplayer1, goalsforplayer2, matchid
Looking for the right query to get the names (not player id) and the result of matches, ideally comparing the goalsforplayer1 and goalsforplayer2 to get either a winner or a draw in each match. Essentially making a list of results.
Is something like this possible using a mysql query?

You should use two inner join on players
select
b.name as nameplayer1
, c.name as nameplayer2
, a.goalsforplayer1
, a.goalsforplayer2
, a.matchid
from matches as a
inner join players as b on a.player1 = b.id
inner join players as c on a.player2 = c.id

This should be what you're looking for (To get a list of players, and their total match results), and should guide you in the correct direction:
SELECT p.*,
(COUNT(CASE WHEN m1.goalsforplayer1 > m1.goalsforplayer2 THEN 1 ELSE NULL END) +
(COUNT(CASE WHEN m2.goalsforplayer2 > m2.goalsforplayer1 THEN 1 ELSE NULL END)
) AS wins
FROM players p
INNER JOIN matches m1 ON p.id = m1.player1
INNER JOIN matches m2 ON p.id = m2.player2
GROUP BY p.id
Join on the matches table, and group by the ID. Use aggregation functions (COUNT here), to get the total number of matches that a player has won. If you want draws, simply modify the queries.
If you're looking for simple results (With players from each side), then use this:
SELECT p1.name AS player1, p2.name AS player2, m.goalsforplayer1, m.goalsforplayer2, m.matchid
FROM matches m
INNER JOIN players p1 ON p1.id = m.player1
INNER JOIN players p2 ON p2.id = m.player2

Related

top 10 scorers by season

How I can get top 10 scorers by seasons.
So it shows last season top 10 scorers...
I've tryed left join into table, but it goes broken showing 2 player and counts all goals to first player.
My sqlfiddle:
http://sqlfiddle.com/#!9/b5d0a78/1
You got it almost right.
You want to group match_goals by player ID (match_player_id), but then you should not select goal_minute or any other per goal data.
After grouping by player, then you can create a column for COUNT(match_player_id) this will give you the number of goals, you can also use this column to order the results.
Your joins and conditions are correct I think.
EDIT
I think your schema needs a few tweaks: check this http://sqlfiddle.com/#!9/f5a75b/2
Basically create direct relations in the match_players and match_goals to the other tables.
I think the query you want looks like this:
SELECT p.*, count(*) as num_goals
FROM match_goals g INNER JOIN
match_players p
ON g.match_player_id = p.id INNER JOIN
matches m
ON m.id = p.match_id
WHERE p.is_deleted = 0 AND
g.is_own_goal = 0 AND
m.seasion_id = <last season id>
GROUP BY p.id
ORDER BY num_goals DESC
LIMIT 10;
Note that the teams table is not needed. The SELECT p.* is allowed because p.id (the GROUP BY key) is unique.

Join two columns from one table to one column from another

I am trying to use my home_team and away_team fixture_id's to get their name values from a different table.
This works to get the name value of home_team
SELECT * FROM fixtures JOIN teams ON fixtures.home_teamID = teams.TeamID
To get the name value of the away team I have the following
SELECT * FROM fixtures JOIN teams ON fixtures.home_teamID = teams.TeamID
JOIN teams ON fixtures.away_teamID = teams.TeamID
But this then returns a boolean
you should join the teams table two time using two different alias
SELECT fixtures.* a.* , b.*
FROM fixtures
JOIN teams a ON fixtures.home_teamID = a.TeamID
JOIN teams b ON fixtures.home_teamID = b.TeamID

MySQL - join two tables, group and count

I have two tables:
reviewStatusPhases - id|name
and
userPhase - id|reviewStatusPhase_id|user_id|created_at|updated_at
The reviewStatusPhases table have records inserted (Active, Inactive, On Pause, Terminated...), and userPhase is empty.
The tables are connected via
userPhase.reviewStatusPhase_id = reviewStatusPhases.id
one to one.
Is it possible that in one query I get all reviewStatusPhases, and cound how many users are in each phase? In this case I will get something like this:
Active (0 Users)
Inactive (0 Users)
On Pause (0 Users)
Terminated (0 Users)
I'm making some assumptions here (e.g. INNER JOIN versus LEFT JOIN in the join, and DISTINCT in the count), but it sounds like you just want
SELECT reviewStatusPhases.name, COUNT(DISTINCT userPhase.user_id)
FROM userPhase INNER JOIN reviewStatusPhases
ON userPhase.reviewStatusPhase_id = reviewStatusPhases.id
GROUP BY reviewStatusPhases.name
Query will be as follows:
SELECT r.name as `name`, count(u.id) as `count` FROM reviewStatusPhases r LEFT OUTER JOIN userPhase u ON r.id = u.reviewStatusPhase_id GROUP BY r.name
left outer join with reviewStatusPhases on left to show all names.
group by names of reviewStatusPhases.
display reviewStatusPhases name and count of user id's (to neglect null values)
Use LEFT JOIN as follows:
SELECT COUNT(m.UserId) FROM Table1 m
LEFT JOIN Table2 k ON k.StatusId = m.StatusId
WHERE k.Status = 'Inactive'
You can easily use the Status column to track the users and their activities. In your case, ReviewStatus.
I hope the following will be helpful
SELECT RPS.Name, COUNT(UP.user_id)
FROM reviewStatusPhases RPS
LEFT OUTER JOIN userphases UP ON RPS.id = UP.reviewStatusPhase_id
GROUP BY RPS.Name
ORDER BY RPS.Name
SELECT
DISTINCT s.s_level AS 'Level',
COUNT(DISTINCT s.s_id) AS Schools,
COUNT(DISTINCT st.st_id) AS Teachers
FROM schools AS s
JOIN school_teachers AS st ON st.st_school_idFk = s.s_id AND st.st_status = 1
WHERE s.s_status = 1
GROUP BY s.s_level

COUNT() rows value in an INNER JOIN

I need to count the amount of games a player has played by counting the number of times his player ID appears on another table, here is the code I've been playing with:
SELECT players.playerID, players.fName, players.lName, matches.playerID, COUNT(matches.playerID) AS 'gamesPlayed'
FROM players
INNER JOIN matches
ON matches.playerID = players.playerID
WHERE players.playerID=matches.playerID
ORDER BY gamesPlayed"
Currently there is 2 players having played 2 matches.
The current code returns 1 player having played 4 matches.
I am trying to get the COUNT(matches.playerID) to return only matches where the players.playerID = matches.playerID.
Any help is much appreciated!
You need to GROUP BY:
SELECT p.playerID, p.fName, p.lName, COUNT(*) AS gamesPlayed
FROM players p INNER JOIN matches m
ON p.playerID = m.playerID
GROUP BY p.playerID, p.fName, p.lName
ORDER BY gamesPlayed
If you want even players who have played zero games, then:
SELECT p.playerID, p.fName, p.lName, COUNT(m.playerID) AS gamesPlayed
FROM players p LEFT JOIN matches m
ON p.playerID = m.playerID
GROUP BY p.playerID, p.fName, p.lName
ORDER BY gamesPlayed
The reason I use COUNT(m.playerID) instead of COUNT(*) in the above is because COUNT(*) will always be at least one in this instance, while COUNT(m.playerID) can be zero if the only matched value is NULL.
At first place: your query lacks GROUP BY, at second - please check what argument COUNT accepts (in this query it's no different from COUNT(*)) – zerkms

Getting an accurate count on this JOIN

This query is working fine. It gives a count of contest entrants for whom the contact id in contest_entries is their origin_contact in the person table.
SELECT c.handle, COUNT(*)
FROM `contest_entry` ce,
person p,
contest c
WHERE
p.origin_contact = ce.contact AND
c.id = ce.contest
GROUP BY c.id
I want to now query how many of those records also have at least one record where the contact id matches in email_list_subscription_log BUT that table may have many log records for any one contact id.
How do I write a join that gives me a count that is not inflated by the multiple records?
Should I use a version of my first query to get all of the contact ids into a tmp table and just use that?
Not sure which field is contact id, but you can do something like this:
select c.handle,
count(*) as count
from `contest_entry` ce
inner join person p on p.origin_contact = ce.contact
inner join contest c on c.id = ce.contest
where exists (
select 1
from email_list_subscription_log l
where l.contact_id = ce.contact
)
group by c.id
You ought to deflate the email_list_subscription_log with DISTINCT or GROUP:
SELECT c.handle, COUNT(*)
FROM `contest_entry` ce
JOIN person p ON (p.origin_contact = ce.contact)
JOIN contest c ON (c.id = ce.contest)
JOIN (SELECT DISTINCT contact, email FROM email_list_subscription_log ) AS elsuniq
ON (ce.contact = elsuniq.contact)
[ WHERE ]
GROUP BY c.id
Using GROUP in the subquery you might count the number of records while still returning one row per element:
JOIN (SELECT contact, count(*) AS elsrecords FROM email_list_subscription_log
GROUPY BY contact) AS elsuniq
With this JOIN syntax, the WHERE is not necessary, but I kept it there if you need additional filtering.