I have a sports site where I have a teams table and a games table. The teams table has the team id, name, and logo(url). The games table has the game id, date, time, hometeamid, and awayteamid.
I'm trying to come up with a mysql query that will return the list of games that includes the associated team names and logos that go with their ids.
Right now, I'm pulling the list of games with their hometeamid and awayteamid, then I'm going back to do a query on the teams table with the hometeamid to get their name and logo, then repeating with the awayteamid to get their name and logo.
That seems like a lot of hitting the db, especially if I can somehow join them in the one first query.
Many thanks for any help you can give.
Let's say you have tables:
TEAM with fields T_ID, NAME, URL
GAME with fields G_ID, T_ID1, T_ID2, others...
A joined select in your case would be:
Select a.G_ID,b.NAME,c.NAME from GAME a, TEAM b, TEAM c where a.T_ID1=c.T_ID and a.T_ID2=b.T_ID
Let us know if you need any more help
Related
I'm working with a database of football games with corresponding dates, locations, a home team, away team, and venue. The games table has keys that representing each of the aforementioned values. I'm currently trying to display each location with with its respective id along with the number of times that it appears throughout the games database as shown below. I coded it like this but for some reason its only displaying one venue and id with a count of the total number instances instead of each individual instance and I can't figure out why.
I'm new to MySQL so I apologize for any mistakes.
SELECT id, venue, count(games.location_id) AS game_count
FROM locations
JOIN games
ON locations.id=games.location_id
you need to add group by as you'are using aggregate function
SELECT venue, count(games.location_id) AS game_count
FROM locations
JOIN games
ON locations.id=games.location_id
group by venue
I have a database that tracks the prizes that people win across the booths of a fair. Most of the booths have overlapping and similar prizes, so a participant can win a Pillow (for example) from 2 booths.
Now, we want to be able to track how many people won a given prize, since we want to tally and account how many people won a certain prize already.
Here is how the table looks like:
It's easy to get the count of a certain prize in a given column. However, I'm having problems trying to consolidate the data across multiple columns.
If you can't normalize your table, a bunch of union alls could do the trick:
SELECT prize, COUNT(*)
FROM (SELECT station_1_prize AS prize FROM mytable
UNION ALL
SELECT station_2_prize AS prize FROM mytable
UNION ALL
-- Etc...
) t
GROUP BY prize
I'm very new to SQL, so please bear with me.
I've built a movie database and I'm trying to query it so that all my tables display properly.
I have a movies table with the columns movieID, title, releaseYear, directorID, genreID, and actorID.
Inside the table director, I have directorID and Director.
Using the query SELECT * FROM movies INNER JOIN director ON director.directorID = movies.directorID;, I'm able to get everything in tables movies and director to display (which isn't exactly what I want, but it's in the right track).
My remaining tables are actor, (with actorID and actor's names) starring (with starringID, movieID, and actorID), genre (with genreID and 22 different genres), and moviegenres (with moviegenresID, moviesID, and genreID).
I'm a bit lost and I apologize if this is confusing and messy, but I'm thinking I need to query the database so that all the tables show the data and are associated with the correct column. For example, most movies have multiple genres and actors, which is why I separated them into tables of their own.
I can't figure out how to query everything to display properly in the result grid.
Thanks in advance
I have trouble to understand how to fetch data from MySQL. Answer might by simple, but i'm stuck, and can't find any help over internet...
I have three tables, let's say first table is named series, second is teams and third id games.
Table structure is something like this:
series:
id
name
teams:
id
name
games:
series_id (relates to series.id)
hometeam_id (relates to teams.id)
visitorteam_id (relates to teams.id)
So my problem is fetching rows from games, where those id's need to be there names, not id's...
Result should be something like this:
"championship
wolverines
marines"
not like
"1
45
142"
Currently i'm fetching those series and teams tables to hashref (in perl) and taking id's from there. But there must be more efficient way to do that in one sql query instead of three.
You need to join the teams table twice (once for home team, second time for visitors)
SELECT g.*,h.name as hometeam_name,v.name as visitorteam_name FROM games g
INNER JOIN series s ON g.series_id=s.id
INNER JOIN teams h ON g.hometeam_id=h.id
INNER JOIN teams v ON g.visitorteam_id=v.id
a team table which containd a team ID and a team name and then 2 further tables that contain data from a quiz and another with data from a questionnaire.
I need to get a list of team IDs that have completed BOTH the quiz (so have some quiz data) AND the questionnaire (so have some questionnaire data) but I can't see how to join the 3 tables to get the list of ids I need.
Any pointers greatly received
As you said in upper comment..
I actually want team IDs that are in either of the data tables. How
would that amend the query? – Dave
Try this, This is what I understood with your words..
select teamID,teamName from team where teamID in (select teamID from quiz) or teamID in (select teamID from questionnaire);
Assuming you have some reference of team table inside quiz and questionaire tables, you will need to make a join this way -
SELECT team.id, team.name
from
team, quiz, questionnaire
where
team.id=quiz.team_id
AND team.id=questionnaire.team_id
when you do team.id=quiz.team_id AND team.id=questionnaire.team_id only those team IDs which are present in both quiz and questionnaire tables willbe shown in the result.
If you need team ID which exists in either of the tables quiz or questionaire, then you will to replace AND by OR-
SELECT team.id, team.name
from
team, quiz, questionnaire
where
team.id=quiz.team_id
OR team.id=questionnaire.team_id