Can anyone help, mysql is beyond me I think!
I have the following tables:
matches
teams
competition
matches holds the details of a game between two teams and the corresponding competition.
I want to query the data base for all games/fixtures for a given competition, I want to return:
team names
team id
team competition
Here is my query:
SELECT
matches.match_id,
teamsh.team_name AS "homeTeam",
teamsa.team_name AS "awayTeam",
competition.competition_id,
competition.name
FROM
matches, teams teamsh, teams teamsa, competition
WHERE
matches.home_team_id = teamsh.team_id
AND matches.away_team_id = teamsa.team_id
AND matches.competition_id=2
For some reason this query returns all fixtures correctly for competition 2, but then it also returns rows for the fixture but as competition 1 also. I can't understand why as I have the clause:
AND matches.competition_id=2
What am I doing wrong, I have checked the database and the matches are stored correctly for each fixture.
Thanks.
You haven't explicitly linked to the competition table (so it's performing a cartesian join) - try adding:
and matches.competition_id = competition.competition_id
- to the end of your query.
Although, I would suggest rewriting the query to use the explicit join syntax - like so:
SELECT m.match_id,
h.team_name AS "homeTeam",
a.team_name AS "awayTeam",
c.competition_id,
c.name
FROM matches m
JOIN teams h ON m.home_team_id = h.team_id
JOIN teams a ON m.away_team_id = a.team_id
JOIN competition c ON m.competition_id = c.competition_id
WHERE m.competition_id=2
You forgot the JOIN condition between the competitions and matches tables.
WHERE
matches.home_team_id = teamsh.team_id
AND matches.away_team_id = teamsa.team_id
AND matches.competition_id = competition.competition_id
AND matches.competition_id=2
Related
In this database
https://www.databasestar.com/sample-database-movies/
I would like to make the following query: "List the name and genre of all the actors in the film Brazil".
I make this query:
USE movies;
SELECT DISTINCT p.person_name AS 'Nombre', g.gender AS 'Sexo' FROM movie m
JOIN movie_crew mc ON m.movie_id = mc.movie_id
JOIN department d ON mc.department_id = d.department_id
JOIN movie_cast mc2 ON m.movie_id = mc2.movie_id
JOIN person p ON mc2.person_id = p.person_id
JOIN gender g ON mc2.gender_id = g.gender_id
WHERE m.title = 'Brazil' AND d.department_name = 'Actors';
But no results appear and I don't understand where is my mistake.
Thanks.
I recommend you simplify the schema somewhat.
Just use simple strings for genre, language_role, keyword, gender, person_name
Use iso_code in place of country_id
Perhaps simple abbreviations for department and company
These do need normalizing (as you have don): person, movie, company, but mostly because there is other stuff in those entities.
That is, get rid of most of the tables in the leftmost and rightmost columns.
Once you have made that change, the error may mysteriously go away. (And, when you get more data, the queries will run faster. This does assume you have suitable indexes.)
I'm pretty new to SQL. I've been googling for hours and I'm stuck with this problem:
I have a table games with following keys:
gameID, homeTeamID, awayTeamID
And a table teams with following keys:
teamID, teamName, teamTLC
Now I want to query a game along with the team names and team tlcs. I guess the query might look something like this:
SELECT games.id, teams.name
FROM games
WHERE games.homeTeamID = 34 OR games.awayTeamID = 34
INNER JOIN teams AS homeTeam
ON leagueGame.homeTeamID = homeTeam.teamID
INNER JOIN teams AS awayTeam
ON leagueGame.awayTeamID = awayTeam.teamID
But I'm pretty sure that this code is completely wrong. Appreciate any help.
You're not that far... :
the WHERE clause comes after the FROM ... JOIN part
as you are querying for a game, you want to filter on gameID rather than on teams
you have no table alias called teams ; you need to use the table aliases that you defined in the JOINs.
Try :
SELECT games.id, homeTeam.name AS homeTeamName, awayTeam.name AS awayTeamName
FROM games
INNER JOIN teams AS homeTeam
ON leagueGame.homeTeamID = homeTeam.teamID
INNER JOIN teams AS awayTeam
ON leagueGame.awayTeamID = awayTeam.teamID
WHERE games.gameID = ?
NB : if you need teamTLC as well, you can access it using table aliases awayTeam and homeTeam.
you need to change the order. first you need to have all your data, then your criteria.
SELECT
games.id,
teams.name
FROM
games
INNER JOIN teams as homeTeam ON leagueGame.homeTeamID = homeTeam.teamID
INNER JOIN teams as awayTeam ON leagueGame.awayTeamID = awayTeam.teamID
WHERE
games.homeTeamID = 34
OR games.awayTeamID = 34
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: match(id.match, date, home, away) and team(id. team, name). In match home and away are foreign keys to the team id. In my query, I want an output record with date of match, and name of these two teams.
I have tried:
SELECT m.date, m.home, m.away, t.name
FROM `match` m
JOIN team t ON m.home = t.id_team
ORDER BY m.date
But this outputs two records instead of one. Is it possible to do what I want with sql or I should just change the table's design?
I suspect you have a many to one relationship between your tables.
This might work depending on the table values:
SELECT DISTINCT m.date, m.home, m.away, t.name
FROM `match` m JOIN
team t ON m.home = t.id_team
ORDER BY m.date
Ok, i did 'deeper' research and find out the answer. Earlier i just asked wrong question. Thanks everyone for contribution!
Here is the solution:
SELECT m.date, m.home, m.away, t.name AS homename, te.name AS awayname
FROM `match` m
JOIN team t ON m.home = t.id_team
JOIN team te
ON m.away = te.id_team ORDER BY m.date
I'm having a bit of a hiccup regarding a particular SQL query. I need to join data from two tables, while also limiting the data (but not necessarily grabbing it) by means of a third table. The tables are as follows:
MEMBERS(member_id,first_name,last_name)
MEMBERS_GROUPS(member_id,group_id)
CHARGES(charge_id,member_id,charge_amount,status)
I need to find all charges for all members of a specific group but I also want to grab the first and last name from the MEMBERS table. The query I've come up with thus far is:
select c.*, m.first_name, m.last_name
FROM charges c
LEFT JOIN member m
ON c.member_id=m.member_id
INNER JOIN members_groups mg
ON mg.group_id=1
i've also tried:
SELECT c.*, m.first_name, m.last_name
FROM charges c, members_groups mg, member m
WHERE c.member_id=mg.member_id
AND mg.group_id = 1
AND c.status='Valid'
AND c.member_id = m.member_id
…but neither returns the data I need. I'm sure I'm overthinking this, but I can't for the life of me get the correct values. I keep getting what appears to be the Cartesian product -- regardless, it's clearly returning too many rows and bad data.
Perhaps what you need is to also restrict the INNER JOIN on members_groups to
those rows with mg.member_id = m.member_id:
SELECT c.*, m.first_name, m.last_name
FROM charges c
LEFT JOIN member m
ON c.member_id=m.member_id
INNER JOIN members_groups mg
ON mg.group_id=1
AND mg.member_id = m.member_id