-1

我试图加入两个具有相同列但值不同的表,但每次我输出它都会重复。

这是我的代码:

<?php
                    $dept = $_SESSION['department'];
                  $dept1 = strtolower($dept);
                  $dept2 = str_replace(" ", "_", $dept1);
                $dept4 = "$dept2" . "_200";
                  $dept = $_SESSION['department'];
                  $level = $_SESSION['level'];
                  $level2 = str_replace (" ", "_", $level);

                  if($level ="200_level") {
                    $query = " SELECT * FROM $dept2 Join $dept4";

                    }
                    else
                    {
                    $query = " SELCT * FROM $dept2";

                    }
                  $result = mysql_query($query) or die('<div       class="header5"small_font">Your Courses are not available yet. Pls contact the ICT Unit</div>');
                      while ($row = mysql_fetch_assoc($result))  {
                        $id = $row['id'];
                   $course = htmlspecialchars($row['course_name']);
                 $code = htmlspecialchars($row['course_code']);
                   $status = $row['status'];
                   $unit = htmlspecialchars($row['unit']);   

   ?>  
4

1 回答 1

0

如果您正在寻找具有相同列的两个表的内容,MySQL UNION应该会有所帮助

UNION 用于将多个 SELECT 语句的结果组合成一个结果集。

像这样:

 $query = "(SELECT * FROM $dept2)
            UNION
           (SELECT * FROM $dept4)";
于 2012-10-23T23:06:18.593 回答