PHP、MySQL - JOIN

PHP, MySQL - JOINs

我是 PHP 的新手,我只是想不通我的代码。我正在使用 MySQL 和 PHP。

table: person PK: personID Other fields: lastName, firstName, hireDate, imgName

table: validMajors PK: majorAbbrev Other Fields: majorDesc

(Junction) table: personMajors personID, majorAbbrev

当我 运行 我的代码(使用 NATURAL JOIN)时,它将显示图像、姓氏和名字以及雇用日期。这太棒了!但我也需要它来显示他们的专业(我希望显示 majorAbbrev)。它也不显示人员 table 但不在 personMajors table 中的人员,这是一个问题,因为我们在人员 table 中有工作人员(他们没有一个专业,因为他们不是学生)

这是我的代码:

<table align="center">
    <?php
        $connection = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME);

        if ( mysqli_connect_errno() ) {
           die( mysqli_connect_error() );  
        }
        $sql = "SELECT * FROM person NATURAL JOIN personMajors ORDER BY lastName";

        if ($result = mysqli_query($connection, $sql)) {     
           // loop through the data

           $columns=4;
           $i = 0;
            while($row = mysqli_fetch_assoc($result))
            {

            if($i % $columns ==0){


                 echo "<tr>";
                 }

                 echo "<td class='staffImage badgeText frameImage displayInLine'>" . "<img src='images/staff/".$row['imgName'].".jpg'>". "<br>". 
                                                            "<strong>" . $row['firstName'] . "</strong>" ." ". 
                                                            "<strong>" . $row['lastName'] . "</strong>" . "<br>" .
                                                            "Hire Date: ".$row['hireDate'] ."</td>";
                                                            "Major: " .$row['majorAbbrev'] ."</td>";     //Does not display

if($i % $columns == ($columns - 1)){
                 echo "</tr>";               
            }

            $i++;
            }
            // release the memory used by the result set
            mysqli_free_result($result); 
        } 
        // close the database connection
        mysqli_close($connection); 
    ?>
    </table>

任何 ideas/solution 将不胜感激!

(第二次尝试):人与主要关系是一对一还是一对多?

好的,这个 SELECT 语句应该有效:

SELECT person.*, validMajors.* FROM person AS p, validMajors AS vm,   personMajors AS pm WHERE p.personID = pm.personID AND pm.majorAbbrev = vm.majorAbbrev 

因为你 concatenating 你的 php 不对。您在显示 $row["lastName"].

后结束了 (;) 您的 echo

您可以试试这些来连接三个表:

SELECT * FROM person
       LEFT JOIN personMajors ON person.personID = personMajors.personID
       LEFT JOIN validMajors ON personMajors.majorAbbrev = validMajors.majorAbbrev

或者您可以定义要在查询中调用的列:

SELECT person.personID,
       person.lastName,
       person.firstName,
       person.hireDate,
       person.imgName,
       validMajors.majorAbbrev,
       validMajors.majorDesc
FROM person
       LEFT JOIN personMajors ON person.personID = personMajors.personID
       LEFT JOIN validMajors ON personMajors.majorAbbrev = validMajors.majorAbbrev

然后你可以用你现在调用它的方式调用结果(更干净的版本):

echo '<td class="staffImage badgeText frameImage displayInLine">
          <img src="images/staff/'.$row["imgName"].'.jpg"><br>
          <strong>'.$row["firstName"].'</strong>
          <strong>'.$row["lastName"].'</strong><br>
          Hire Date: '.$row["hireDate"].'
          Major: '.$row["majorAbbrev"].'
      </td>';