I have 2 SELECT queries one which retrieve a team and one which count how many players there are in that team. How can i add that count query in to the first data select query? i've tried by union, but does not work since they do not have the same number of columns.
SELECT Subteams.id, Teams.name, Games.name as 'game'
FROM Subteams, Games, Teams
WHERE Subteams.gameId = Games.id
AND Subteams.teamId = Teams.id
SELECT COUNT(Players.subTeamId) as 'count'
FROM Subteams, Games, Teams, Players
WHERE Subteams.gameId = Games.id
AND Subteams.teamId = Teams.id
AND Players.subTeamId = Subteams.id
ORDER BY Players.id
Just add the columns from the other tables to the COUNT() query.
SELECT Subteams.id, Teams.name, Games.name as game, COUNT(Players.subTeamId) as 'count'
FROM Subteams
JOIN Games ON Subteams.gameId = Games.id
JOIN Teams ON Subteams.teamId = Teams.id
LEFT JOIN Players ON Players.subTeamId = Subteams.id
GROUP BY Subteams.id
ORDER BY Subteams.id
I've also used LEFT JOIN so you'll get 0 for teams with no players.
Related
SELECT Count(Teams.Name)
, Teams.TeamID
, Teams.Name
FROM Teams, Rosters
WHERE Rosters.TeamID = Teams.TeamID
Is my query so far. I'd like it to print the number of entries of Rosters.TeamID that correspond to Teams.TeamID.
I think the query you want is:
SELECT t.TeamID, t.Name, COUNT(*)
FROM Teams t JOIN
Rosters r
ON r.TeamID = t.TeamID
GROUP BY t.TeamID, t.Name;
Basically, your query is missing the GROUP BY, but you should also learn proper JOIN syntax.
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
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
I have 3 tables, players, matches, wins. I want to make a query that retrieves back the player id, the name, the amount of matches he played and how many wins he got.
the matches table keeps track of the round it's in, the players who take part (p_one_id and p_two_id) while the wins table keeps track of the match id, to see who he won against, and the player id.
if I do
SELECT players.id, players.name, COUNT(matches.*) as matches FROM players, matches WERE matches.p_one_id = players.id GROUP BY players.id
It works fine, at least from my basic tests ( but that doesn t also check for wins) but if I do
SELECT players.id, players.name, COUNT(matches.*) as matches, COUNT(wins.*) AS wins FROM players, matches, wins WERE matches.p_one_id = players.id AND wins.p_id = players.id GROUP BY players.id
I get nothing, one reason is that there's no wins, but then I d only get the guys who won, I can t make a sub query either, since I can't use the player id inside of the subquery, at least not from my knowledge). Anyone know what query I'd need for my result?
I would suggest using a LEFT JOIN instead of the cross join / where clause option. You may also wish to count the matches where the player was "player 2". Something along the lines of:
SELECT
p.id,
p.name,
count(m1.*) + count(m2.*) as matches,
count(w.*) as wins
FROM players p
LEFT JOIN matches m1
ON m.p_one_id = p.id
LEFT JOIN matches m2
on m.p_two_id = p.id
LEFT JOIN wins w
ON w.p_id = p.id
GROUP BY p.id, p.name
User outer joins. The join syntax you're using is eliminating players unless they have both wins and matches.
SELECT players.id, players.name,
COUNT(matches.p_one_id) as matches,
COUNT(wins.p_id) AS wins
FROM players
LEFT JOIN matches
on matches.p_one_id = players.id
LEFT JOIN wins
on wins.p_id = players.id
GROUP BY players.i, players.name
This will return all players, matches, and wins.
I need to display a table that lists a game's title, developer(s) and publisher(s). Each game can have multiple developers and publishers. From the below diagram, is it possible to get all this information through one query using inner joins or will I have to make multiple database transactions?
As it stands I have been able to obtain a game's developer (singular) but I'm not sure how to grab multiple developers as well as multiple publishers in the one query. This is my query so far:
SELECT games.id, games.title, companies.id, companies.name
INNER JOIN game_developer ON game_developer.games_id = games.id
INNER JOIN companies ON companies.id = game_developer.companies_id
GROUP BY games.title
ORDER BY games.title
LIMIT 50
The final product should look something like this:
Use GROUP_CONCAT to get all the names:
SELECT games.id, games.title,
GROUP_CONCAT(DISTINCT dc.name) AS developers,
GROUP_CONCAT(DISTINCT pc.name) AS publishers
INNER JOIN game_developer ON game_developer.games_id = games.id
INNER JOIN companies AS dc ON dc.id = game_developer.companies_id
INNER JOIN game_publisher AS gp ON gp.games_id = games.id
INNER JOIN companies AS dp ON dp.id = gp.companies_id
GROUP BY games.id
ORDER BY games.title
LIMIT 50
You need to use DISTINCT because joining with both publishers and developers will generate a cross product with lots of duplicates.