简体   繁体   English

如何使用另一个表的ID显示一个表的属性

[英]How to display an attribute from one table using an ID from another

I have the following tables in an Oracle database: 我在Oracle数据库中具有以下表:

CREATE TABLE Team
 (
  teamID INT NOT NULL,
  teamName VARCHAR(50),
  wins INT,
  losses INT,
  otlosses INT,
  points INT,
  PRIMARY KEY (teamID)
);


    CREATE TABLE Matchup
       (
           matchID INT NOT NULL,
           roundID INT NOT NULL,
            team1ID INT NOT NULL,
            team2ID INT NOT NULL,
            PRIMARY KEY (matchID),
            FOREIGN KEY (roundID) REFERENCES Round (roundID),
            FOREIGN KEY (team1ID) REFERENCES Team (teamID),
            FOREIGN KEY (team2ID) REFERENCES Team (teamID) 
         );

I am looping through the matchup table and displaying all of the matchups. 我正在遍历对决表并显示所有对局。 I can only get it to display the TeamID however and am trying to get it to display the teamName from the Team table using the IDs I am getting from the Matchup table. 我只能让它显示TeamID,并试图使用从Matchup表中获取的ID使其从Team表中显示teamName。

this is what I am trying so far but can't seem to get it to work. 到目前为止,这是我一直在尝试的方法,但似乎无法使其正常工作。

 $query = 'SELECT team1ID, team2ID  FROM Matchup WHERE RoundID=1 IN (SELECT teamName FROM Team WHERE team1ID=teamID AND team2ID=teamID)';

this is the rest of the php code which is supposed to display the information on my html page 这是应该在我的html页面上显示信息的php代码的其余部分

 <h2>Round 1 Matchups</h2>
<table width=100%>
<tr>
    <th title="team1ID">Team</th>
    <th title="team2ID">Team</th>

</tr>

             <?php
           // Remember to replace 'username' and 'password'!
          $conn = oci_connect('xx', 'xxx', '(DESCRIPTION=(ADDRESS_LIST=
          (ADDRESS=(PROTOCOL=TCP)(Host=db2.ndsu.edu)(Port=1521)))
          (CONNECT_DATA=(SID=cs)))');


         //put your query here
        $query = 'SELECT team1ID, team2ID  FROM Matchup WHERE RoundID=1 IN 
        (SELECT teamName FROM Team WHERE team1ID=teamID AND 
         team2ID=teamID)';
        $stid = oci_parse($conn,$query);
        oci_execute($stid,OCI_DEFAULT);

        //iterate through each row
        while ($row = oci_fetch_array($stid,OCI_ASSOC)) 
        {
         echo '<tr>';
         foreach ($row as $item)
        {
          echo '<td>' . $item . '</td>';
         }
          echo '</tr>';

        echo '<br>';}
        echo '</table>';
        oci_free_statement($stid);
       oci_close($conn);
         ?>

You can join twice on the team table twice, once for each team: 您可以在team表上两次加入两次,每个团队一次:

SELECT t1.name, t2.name
FROM   match m
JOIN   team t1 ON t1.teamid = m.team1id
JOIN   team t2 ON t2.teamid = m.team2id
WHERE  roundid = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM