如何使用PHP将带有外键的多个MYSQL表实现到HTML表中?

时间:2014-03-30 19:44:21

标签: php html mysql foreign-keys

我是新的SQL和PHP,我尝试实现一个显示3个表中内容的表(所有表都链接到包含外键的另一个表)

我的主要表格包括job_idstaff_id*series_id*channel_id*。所有*都链接到另一个表,它们是主键。我希望使用PHP在一个大表中显示所有其他表信息。

到目前为止,我能够在没有任何其他表信息的情况下显示我的主表,以下内容,我不知道从哪里开始,因为这对我来说都是新的。

    <?php
$dbconnect=mysqli_connect('localhost', 'user', 'pass', 'database');
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

 $result = mysqli_query($dbconnect,"SELECT * FROM JOB_LIST");
 echo "<table border='1'>
 <tr>
 <th>Job ID</th>
 <th>Staff ID</th>
 <th>Channel ID</th>
 <th>Series ID</th>
 </tr>";
 while($row = mysqli_fetch_array($result))
   {
   echo "<tr>";
   echo "<td>" . $row['JOB_STAFF'] . "</td>";
   echo "<td>" . $row['JOB_CHANNEL'] . "</td>";
   echo "<td>" . $row['JOB_SERIES'] . "</td>";
   echo "</tr>";
   }
 echo "</table>";
 mysqli_close($dbconnect);
 ?> 

我需要从JOB_LIST(主要链接表),STAFF_LISTSERIES_LIST&amp; CHANNEL_LIST。最终结果表行应为:

echo "<td>" . $row['JOB_STAFF'] . "</td>"; //REFERENCES STAFF_ID
echo "<td>" . $row['STAFF_FIRSTNAME'] . "</td>";
echo "<td>" . $row['STAFF_SURNAME'] . "</td>";
echo "<td>" . $row['STAFF_EMAIL'] . "</td>"; 
echo "<td>" . $row['JOB_CHANNEL'] . "</td>"; //REFERENCES CHANNEL_ID
echo "<td>" . $row['CHANNEL_NAME'] . "</td>";
echo "<td>" . $row['CHANNEL_NETWORK'] . "</td>";    
echo "<td>" . $row['JOB_SERIES'] . "</td>"; //REFERENCES SERIES_ID
echo "<td>" . $row['SERIES_NAME'] . "</td>";
echo "<td>" . $row['SERIES_COUNT'] . "</td>";

但我不知道需要改变什么才能实现它。如果我没有解释某事,我道歉会尝试更好地解释一些事情。

1 个答案:

答案 0 :(得分:1)

正如已经说过LEFT OUTER JOIN就是你所需要的。

关于加入的几句话

两个表之间没有任何条件的简单CROSS JOIN只不过是交叉产品。列将被添加到一起,行将成倍增加。因此,如果您有一个包含3列和20行的表,另一个包含5列和1000行,则连接表将具有3 + 5 = 8列和20 * 1000 = 20,000行。

通常你不需要整个产品。这就是为什么你可以利用条件。 INNER JOIN就像一个CROSS JOIN,但有一个额外的ON关键字定义了连接条件,以减少所需行的结果数。这意味着每一行都不符合条件的行被整理出来。从技术上讲,它与WHERE条款完全相同,但你不应该把它混合起来,因为在语义上它是一个非常不同的野兽。通常,连接条件与主键匹配。

可以执行多个连接,其中第一个表与第二个表连接的结果将与第三个表连接,依此类推。

SELECT * FROM table_1 AS t1
    INNER JOIN table_2 AS t2
    ON t1.foreign_key_1 = t2.primary_key
    INNER JOIN table_3 AS t3
    ON t1.foreign_key_2 = t3.primary_key;

如您所见,我还使用AS关键字为表提供别名。这很重要,因为不同的表可能具有相同的字段名称。这些所谓的限定名称可以避免任何可能的名称冲突。

但INNER JOIN仍然不是你想要的。您有以下情况:

您希望输出表中有一行用于每个不同的作业ID。它应始终存在,无论它是引用整个外键集(staff_id,channel_id,series_id)还是仅引用一些外键(例如, staff_id和channel_id或根本没有条目。

INNER JOIN将拒绝所有这些部分行,因为对于连接的两个边(表),ON条件必须为true。幸运的是,LEFT OUTER JOIN确保左侧的表将保留其所有行,并查找与右表中的ON条件匹配的表。行中无法匹配的列将填充NULL值。正如你可能已经猜到的那样,正确的外部联接正好相反。

嗯,这是您需要的查询。

SELECT li.id AS job_id, st.id AS staff_id, st.firstname AS staff_firstname, st.surname AS staff_surname,
    st.email AS staff_email, ch.id AS channel_id, ch.name AS channel_name,
    ch.network AS channel_network, se.id AS series_id, se.name AS series_name,
    se.count AS series_count
    FROM job_list AS li
    LEFT OUTER JOIN job_staff AS st
    ON li.staff_id = st.id
    LEFT OUTER JOIN job_channel AS ch
    ON li.channel_id = ch.id
    LEFT OUTER JOIN job_series AS se
    ON li.series_id = se.id;

这里是更新的PHP部分。您可能需要根据需要调整一些列和表名称,因为我无法预测您的数据库名称和表格模式。但基于我所做的这个SQL Fiddle,它应该按照你想要的方式工作。滚动到底部,您会看到查询产生的表格。

$dbconnect=mysqli_connect('localhost', 'user', 'pass', 'test');

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query = "SELECT li.id AS job_id, st.id AS staff_id, st.firstname AS staff_firstname, st.surname AS staff_surname,
            st.email AS staff_email, ch.id AS channel_id, ch.name AS channel_name,
            ch.network AS channel_network, se.id AS series_id, se.name AS series_name,
            se.count AS series_count
            FROM job_list AS li
            LEFT OUTER JOIN job_staff AS st
            ON li.staff_id = st.id
            LEFT OUTER JOIN job_channel AS ch
            ON li.channel_id = ch.id
            LEFT OUTER JOIN job_series AS se
            ON li.series_id = se.id;";

$result = mysqli_query($dbconnect, $query);

echo "<table border='1'>
<tr>
    <th>Job ID</th>
    <th>Staff ID</th>
    <th>Staff Firstname</th>
    <th>Staff Surname</th>
    <th>Staff E-mail</th>
    <th>Channel ID</th>
    <th>Channel Name</th>
    <th>Channel Network</th>
    <th>Series ID</th>
    <th>Series Name</th>
    <th>Series Count</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td>" . $row['job_id'] . "</td>";
    echo "<td>" . $row['staff_id'] . "</td>";
    echo "<td>" . $row['staff_firstname'] . "</td>";
    echo "<td>" . $row['staff_surname'] . "</td>";
    echo "<td>" . $row['staff_email'] . "</td>";
    echo "<td>" . $row['channel_id'] . "</td>";
    echo "<td>" . $row['channel_name'] . "</td>";
    echo "<td>" . $row['channel_network'] . "</td>";
    echo "<td>" . $row['series_id'] . "</td>";
    echo "<td>" . $row['series_name'] . "</td>";
    echo "<td>" . $row['series_count'] . "</td>";
    echo "</tr>";
}
echo "</table>";

mysqli_close($dbconnect);

我希望我可以照亮一下加入。如果有什么不清楚但随意问。