PHP MYSQL JOIN:两个相似的表

时间:2012-10-23 10:49:20

标签: php mysql join

尝试连接两个具有相同列但值不同的表,但每次输出它都是重复的。

这是我的代码:

<?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']);   

   ?>  

1 个答案:

答案 0 :(得分:0)

如果您希望获得具有相同列的两个表的内容,MySQL UNION应该有帮助

  

UNION用于将多个SELECT语句的结果合并到一个结果集中。

像这样:

 $query = "(SELECT * FROM $dept2)
            UNION
           (SELECT * FROM $dept4)";