数据库
首页 > 数据库> > mysql – SQL查询连接无法正常工作

mysql – SQL查询连接无法正常工作

作者:互联网

对于一个项目,我需要为小型奥运会创建一个数据库.我的ER图如图所示……
enter image description here
链接到更好的图片:https://i.imgur.com/xgfurWO.png?1

我需要创建一个查询,其中只包含竞争多个竞争对手的竞争对手(只有两个竞争对手),并列出名称,事件,地点和结果.

对于竞争对手参加两项活动,下面的查询可以产生70(竞争对手的#)记录,计数为2 …

SELECT c.firstname, c.lastname, COUNT(r.competitorid)
FROM  COMPETITOR c LEFT OUTER JOIN ( REGISTRATION r LEFT OUTER JOIN  EVENT e 
               ON r.eventid = e.eventid ) ON r.competitorid = c.competitorid
GROUP BY c.firstname, c.lastname
ORDER BY c.firstname;
-----------------------------------------
NORRIS  HOLMWOOD    1
OCTAVIO MARTINEZ    1
ORFEO   SILVA       2
etc...

在查询中包含事件名称后,它会生成72个结果,竞争对手在两个列出的事件中竞争两次,但COUNT()对于所有事件都是1.

SELECT c.firstname, c.lastname, e.eventname, COUNT(r.competitorid)
FROM  COMPETITOR c LEFT OUTER JOIN ( REGISTRATION r LEFT OUTER JOIN  EVENT e 
ON r.eventid = e.eventid ) ON r.competitorid = c.competitorid
GROUP BY c.firstname, c.lastname, e.eventname
ORDER BY c.firstname;
----------------------------------------------------------------
NORRIS  HOLMWOOD    100 METER BUTTERFLY 1
OCTAVIO MARTINEZ    FLOOR EXERCISE  1
ORFEO   SILVA   100 METER BUTTERFLY 1
ORFEO   SILVA   400 METER INDIV MEDLEY  1
PONCIO  ASIS    POMMEL HORSE    1
PONCIO  BARROS  LONG JUMP   1 

然后这就是事情真的搞砸了.当我将结果表与事件表连接时,输出与72条记录保持相同,但计数完全错误.

SELECT c.firstname, c.lastname, e.eventname, COUNT(r.competitorid)
FROM  COMPETITOR c LEFT OUTER JOIN ( REGISTRATION r LEFT OUTER JOIN (
    EVENT e right OUTER JOIN RESULT rs ON e.eventid = rs.eventid)
    ON r.eventid = e.eventid ) ON r.competitorid = c.competitorid
GROUP BY c.firstname, c.lastname, e.eventname
ORDER BY c.firstname;
-----------------------------------------------------------------------
NIKOLAI MIKHAILOV   POMMEL HORSE        6
NOEMI   PELAEZ  BALANCE BEAM            7
NORRIS  HOLMWOOD    100 METER BUTTERFLY 6
OCTAVIO MARTINEZ    FLOOR EXERCISE      6
ORFEO   SILVA   100 METER BUTTERFLY     6
ORFEO   SILVA   400 METER INDIV MEDLEY  6
PONCIO  ASIS    POMMEL HORSE            6

我的问题是我做错了什么?连接似乎至少部分地工作我想要它们,问题是乱搞count().数字越大并不一定重要,问题是由于某种原因,计数似乎相当随机.

解决方法:

考虑:(结果也特定于竞争对手,因此您错过了加入标准.)
此外,右连接意味着您希望结果中的所有记录和其他表中的所有记录都是LEFT加入竞争对手.由于结果在逻辑上基于竞争对手,我猜测正确的连接是不正确的.

因此,在计数1上出现双重问题,缺失的连接标准导致计数膨胀,而正确的连接可能也会导致问题,但由于结果取决于竞争对手,因此您可能看不到任何问题.

如果你想要竞争对手注册的事件数量,你需要分别得出那个数量[绝对拼写错误太可笑了,不能取出]然后加入(有其他方法,但我通常更喜欢这种方法,因为它似乎是DB不可知的)

SELECT c.firstname, c.lastname, e.eventname, CE.TotalNumberOFEvents
FROM  COMPETITOR c 
LEFT OUTER JOIN REGISTRATION r 
  ON r.competitorid = c.competitorid
LEFT OUTER JOIN EVENT e 
  ON r.eventid = e.eventid 
LEFT OUTER JOIN RESULT rs    --Not sure why this is a right join... on your qry.
  ON e.eventid = rs.eventid
 AND Rs.CompetitorID = C.CompetitorID  ---ADDED this and removed the ()'s putting join critier under the joins. 
LEFT JOIN (SELECT count(RegistrationID) as TotalNumberOfEvents, CompetitorID 
           FROM Registration) as CE    --added this to get the # of events a competitor is in (independant of the event name listed)
  ON Ce.CompetitorID = C.CompetitorID
GROUP BY c.firstname, c.lastname, e.eventname
ORDER BY c.firstname;

标签:mysql,join,oracle-sqldeveloper
来源: https://codeday.me/bug/20190706/1394680.html