I'm creating a database that is going to be used for a sporting competition. To view the results of a game, I need to join six tables together:
Season s
Round r
Rounddetails rd
Match m
Matchdetails md
Game g
I want the select statement to return something like this:
g.gameid, g.pointsfor, g.pointsagainst, md.matchdetailsid, m.matchid, rd.rounddetailsid, r.roundid, s.seasonid
Could anyone help me write a MySQL statement that will return that?
Please click the link to view the tables
EDIT: so far I have tried this query:
select
`s`.`seasonID` AS `seasonID`,
`r`.`roundID` AS `roundid`,
`rd`.`roundDetailsID` AS `roundDetailsID`,
`m`.`matchid` AS `matchid`,
`md`.`matchDetailsID` AS `matchDetailsID`,
`g`.`gameid` AS `gameid`,
`g`.`pointsfor` AS `pointsfor`,
`g`.`pointsagainst` AS `pointsagainst`
from (((((`season` `s` left join `round` `r` on((`s`.`SeasonID` = `r`.`SeasonID`)))
left join `rounddetails` `rd` on((`r`.`RoundID` = `rd`.`RoundID`)))
left join `match` `m` on((`rd`.`matchid` = `m`.`matchid`)))
left join `matchdetails` `md` on((`m`.`matchid` = `md`.`matchid`)))
left join `game` `g` on((`md`.`matchdetailsid` = `g`.`matchdetailsid`)))
SELECT DISTINCT g.gameid, g.pointsfor, g.pointsagainst, md.matchdetailsid, m.matchid, rd.rounddetailsid, r.roundid, s.seasonid
FROM Match AS m
INNER JOIN MatchDetails as md
ON Match.MatchId = MatchDetails.MatchId
INNER JOIN Game as g
ON MatchDetails.MatchDetailSid = MatchDetailSid
INNER JOIN RoundDetails as rd
ON Match.MatchId = RoundDetails.MatchId
INNER JOIN Round as r
ON Round.RoundId = RoundDetails.RoundId
INNER JOIN Season as s
ON Round.SeasonId = Season.SeasonId
WHERE MatchId = 1
Related
I made a query that list all the players on all the teams for each of the clubs in my site.
A "club" has "teams" and each team has "players", I solve the listing using this query:
SELECT
Club.*,
Teams.*,
Players.*
FROM
(Club LEFT JOIN Teams ON Club.idClub = Teams.idClub)
LEFT JOIN
Teams ON Teams.idEquipos_Categorias = Players.idEquipos_Categorias
WHERE
Teams.idTemporadas = 2017
It works great, now I want to list just the players than "has played" from other table "hasPlay" I made this changes, but It returns empty:
SELECT
Club.*,
Teams.*,
Players.*,
HasPlay.*
FROM
((Club LEFT JOIN Teams ON Club.idClub = Teams.idClub)
LEFT JOIN
Teams ON Teams.idEquipos_Categorias = Players.idEquipos_Categorias) LEFT JOIN HasPLay ON Players.idPlayer = HasPlay.idPlayer
WHERE
Teams.idTemporadas = 2017
I think I mess it up in the last join...
Any advice is well recieved
I doubt your original query works
as it contains various errors and anomalies
anyway you should avoid useless ()
and especially use JOIN on the Equipos_Categorias table eg:
SELECT
Club.*,
Teams.*,
Players.*,
HasPlay.*
FROM Club LEFT JOIN Teams ON Club.idClub = Teams.idClub
LEFT JOIN Teams ON Teams.idEquipos_Categorias = Players.idEquipos_Categorias
LEFT JOIN HasPLay ON Players.idPlayer = HasPlay.idPlayer
INNER JOIN Equipos_Categorias ON Teams.idEquipos_Categorias = Equipos_Categorias.idEquipos_Categorias
and Equipos_Categorias.idTemporadas = 2017
ORDER BY Club.nombre, Equipos_Categorias.idEquipos_Categorias ASC,
Jugadores_Equipos.categoriaJugador ASC, Jugadores_Equipos.nombre ASC
and as verified by OP uisng team table
SELECT
Club.*,
Teams.*,
Players.*,
HasPlay.*
FROM Club LEFT JOIN Teams ON Club.idClub = Teams.idClub
INNER JOIN Teams ON Teams.idEquipos_Categorias = Players.idEquipos_Categorias and team.idTemporadas = 2017
LEFT JOIN HasPLay ON Players.idPlayer = HasPlay.idPlayer
ORDER BY Club.nombre, Equipos_Categorias.idEquipos_Categorias ASC,
Jugadores_Equipos.categoriaJugador ASC, Jugadores_Equipos.nombre ASC
Need a little help with subqueries
If I have a 1 query like this:
SELECT Learner.Learner_Id, Max(LearnerEmploymentStatus.DateEmpStatApp) AS LatestEmpDate, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON LearnerEmploymentStatus.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id
WHERE EmploymentStatusMonitoring.ESMType="BSI"
GROUP BY Learner.Learner_Id, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType
...and another like this:
SELECT Learner.Learner_Id, LearnerEmploymentStatus.DateEmpStatApp, EmploymentStatusMonitoring.ESMCode
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON Learner.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id
...and I wanted to do a join between the 2 queries (LEFT JOIN on the common Learner_Id and LatestEmpDate / DateEmpStatApp fields), how would I go about doing all this work in a single query where the 2 queries above would be subqueries?
My attempt below is not being accepted (JOIN expression not supported):
SELECT sQ1.Learner_Id, sQ1.LearnRefNumber, sQ1.FamilyName, sQ1.GivenNames, sQ1.LatestEmpDate, sQ1.ESMType, sQ2.ESMCode
FROM
(SELECT Learner.Learner_Id, Max(LearnerEmploymentStatus.DateEmpStatApp) AS LatestEmpDate, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON LearnerEmploymentStatus.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id
WHERE EmploymentStatusMonitoring.ESMType="BSI"
GROUP BY Learner.Learner_Id, Learner.LearnRefNumber, Learner.FamilyName, Learner.GivenNames, EmploymentStatusMonitoring.ESMType) As sQ1
LEFT JOIN
(SELECT Learner.Learner_Id, LearnerEmploymentStatus.DateEmpStatApp, EmploymentStatusMonitoring.ESMCode
FROM (Learner LEFT JOIN LearnerEmploymentStatus ON Learner.Learner_Id = LearnerEmploymentStatus.Learner_Id) LEFT JOIN EmploymentStatusMonitoring ON LearnerEmploymentStatus.LearnerEmploymentStatus_Id = EmploymentStatusMonitoring.LearnerEmploymentStatus_Id) As sQ2
ON (sQ1.Learner_Id = sQ2.Learner_Id) AND (sQ1.LatestEmpDate = sQ2.DateEmpStatApp);
Would something like this get you what you want...?
SELECT l.Learner_Id, d.LatestEmpDate, l.LearnRefNumber, l.FamilyName, l.GivenNames, m.ESMType, m.ESMCode
FROM ((Learner AS l
LEFT JOIN (
SELECT s.Learner_Id, MAX(s.DateEmpStatApp) AS LatestEmpDate
FROM LearnerEmploymentStatus AS s
GROUP BY s.Learner_Id) AS d ON d.Learner_Id = l.Learner_Id)
LEFT JOIN LearnerEmploymentStatus AS ls ON (ls.Learner_Id = d.Learner_Id) AND (ls.DateEmpStatApp = d.LatestEmpDate))
LEFT JOIN EmploymentStatusMonitoring AS m ON m.LearnerEmploymentStatus_Id = ls.LearnerEmploymentStatus_Id
WHERE m.ESMType = 'BSI'
Assumes the same learner won't have the same DateEmpStatApp twice, which may or not be valid.
I've got the query below that's pulling data from a number of tables to create an update:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
It works fine but when there are plenty of records in papr_down then it takes ages to process. Any ideas about how it can be optimized?
What I think is the join between the emailAddress is the issue here, you can try out with the join with the Id's.
If you provide us the screen shot of the below query ::
EXPLAIN Select * from
en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
WHERE sd.fieldid = 33
As I know we should use joins only for the columns which are preset in SELECT clause and for other joins we should implement using WHERE clause
Please try following query:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls
on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down AS pd1
INNER JOIN email.papr_data AS pd2 on pd1.paper_id = pd2.id
WHERE
email.papr_exam.id in (select exam from email.papr_data where exam = 1)
AND
email.papr_levl.id in (select level from email.papr_data where level = 4 )
AND
email.papr_subj.id in (select subject from email.papr_data)
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
I can not execute this at my machine since i don't have the schema
I have a problem in a query.
i need to make a page where user can see all reservations he made with movie name, cinema name, seats code that he reserved
i reached this level
SELECT member.member_username, show_datetime, movie_name
FROM `member`
JOIN `reservation` ON `reservation`.`member_id` = `member`.`member_id`
JOIN `show` ON `show`.`show_id`= `reservation`.`show_id`
JOIN `movie` ON `movie`.`movie_id` = `show`.`movie_id`
WHERE `reservation`.`member_id`=1
i need to make a connection and get also the cinema_name from cinema table and seats_code from seat table and theater name
in fact, i need a query to give me all almost all data in my whole database
here is the schema for the DB
http://imageshack.us/photo/my-images/824/58054944.jpg/
JOIN these two tables too:
SELECT
m.member_username,
sh.show_datetime,
v.movie_name,
c.cinema_name,
t.theater_name
FROM member AS m
INNER JOIN reservation AS r ON r.member_id = m.member_id
INNEr JOIN show AS sh ON sh.show_id = r.show_id
INNER JOIN movie AS v ON v.movie_id = s.movie_id
INNER JOIN theater AS t ON t.theater_id = sh.theater_id
INNER JOIN cinema AS c ON c.theater_id = sh.theater_id
WHERE r.member_id = 1
Keep joining your tables
SELECT member.member_username, show_datetime, movie_name, c.cinema_name, t.theater_name
FROM `member`
JOIN `reservation` ON `reservation`.`member_id` = `member`.`member_id`
JOIN `show` ON `show`.`show_id`= `reservation`.`show_id`
JOIN `movie` ON `movie`.`movie_id` = `show`.`movie_id`
JOIN `theater` ON `show`.`theater_id` = `theater`.`theater_id`
JOIN `cinema` ON `theater`.`cinema_id` = `cinema`.`cinema_id`
JOIN `seat` ON `show`.`theater_id` = `seat`.`theater_id`
WHERE `reservation`.`member_id`=1
I am running a query:
select
iars.id,students.rollno,
students.name as name,
teachers.name as tname,
students.studentid,
t1.studentid,
sum(t1.obt) as obt1, sum(t1.benefits) as ben1, sum(t1.max) as max1,
(t2.obt) as obt2, (t2.benefits) as ben2, (t2.max) as max2,
(t3.obt) as obt3, (t3.benefits) as ben3, (t3.max) as max3,
(t4.obt) as obt4, (t4.benefits) as ben4, (t4.max) as max4,
(t5.obt) as obt5, (t5.benefits) as ben5, (t5.max) as max5
from groups,students
left join iars on iars.id
left join str on str.studentid=students.studentid
left join course on course.c_id=students.course
left join teachers on teachers.id=iars.teacherid
join sgm on sgm.studentid=students.studentid
left join semdef on month
left join sps on sps.studentid=students.studentid and iars.paperid=sps.paperid
left join `attndata` `t1` on ((t1.studentid=students.studentid) and iars.id=t1.iarsid and t1.mon=1)
left join `attndata` `t2` on ((t1.studentid=t2.studentid) and t2.mon = 2 and iars.id=t2.iarsid)
left join `attndata` `t3` on ((t2.studentid=t3.studentid) and t3.mon = 4 and iars.id=t3.iarsid)
left join `attndata` `t4` on ((t3.studentid=t4.studentid) and t4.mon = 5 and iars.id=t4.iarsid)
left join `attndata` `t5` on ((t4.studentid=t5.studentid) and t5.mon = 6 and iars.id=t5.iarsid)
where students.course='1' and students.status='regular' and sps.paperid='2' and
iars.courseid=students.course and iars.semester=str.semesterid and iars.paperid='2' and
str.semesterid='1' and str.sessionid='12' and groups.id=sgm.groupid
group by sps.studentid , teachers.id order by students.name
It's with grouping, like I want to have sum according to the months, but whenever I use sum, it gives me funny result.
If you want the sum by months, you should group by date with month. Grouping with student and teacher will give you more than one row per month and will result in funny result