避免显示select语句中的重复值

时间:2013-11-23 07:34:36

标签: php mysql

我正在使用foreach循环来获取结果,但是我显示了不必要的重复结果。下面你会看到我的价值出现了两倍以上。在我的查询select语句中,我在表之间进行内连接。然后我做一个foreach循环来显示这些结果。结果显示在三个表中:academy,courses_by_academy和person。他们都共享一个外键学院_id。我不确定这是否与查询或foreach循环的格式有关。如何在不重复显示的情况下显示如下所示的信息?

PHP

$db_select  = $db_con->prepare("
SELECT a.name, 
       a.academy_id,
       ca.course_name,
       ca.course_start_date,
       ca.course_end_date,
       p.contact_role,
       p.instructor_role,
       p.first_name,
       p.last_name,
       p.person_email,
       p.person_phone,
       p.person_fax
FROM academy a
INNER JOIN courses_by_academy ca ON a.academy_id = ca.academy_id
INNER JOIN person p ON a.academy_id = p.academy_id
WHERE a.academy_id = :acad_id
");
if (!$db_select) return false;
    if (!$db_select->execute(array(':acad_id' => $acad_id))) return false;
    $results = $db_select->fetchAll(\PDO::FETCH_ASSOC);
    if (empty($results)) return false;
    $final_result = '';
    $first = true;
    foreach ($results as $value){
        if($first){
          $first = false;
          $final_result .= "<b>Academy Name: </b>".$value['name']."<b>  ID: </b>".$value['academy_id']."</br>";
        }
          $final_result .= "-------------------COURSES_OFFERED------------------</br>";
          $final_result .= "<b>Course Name: </b>".$value['course_name']."</br><b>Start Date: </b>".$value['course_start_date']."</br><b>End Date: </b>".$value['course_end_date']."</br>";
          $final_result .= "---------------------PERSONEL-----------------------</br>";
          $final_result .= "<b>First Name: </b>".$value['first_name']."</br><b>Last Name: </b>".$value['last_name']."</br><b>Email: </b>".$value['person_email']."</br>";
          $final_result .= "<b>This person has the role of an instructor: </b>".$value['instructor_role']."</br><b>This person has the role of a contact: </b>".$value['contact_role']."</br>";
          $final_result .= "<b>Phone: </b>".$value['person_phone']."</br><b>Fax: </b>".$value['person_fax']."</br>";        
    }

}

希望的展示方式 -

-------------------COURSES_OFFERED------------------
Course Name: Biology
Start Date: 2013-11-13
End Date: 2013-11-26
-------------------COURSES_OFFERED------------------
Course Name: Calculus
Start Date: 2013-11-19
End Date: 2013-11-16
---------------------PERSONEL-----------------------
First Name: Person1
Last Name: Last1
Email: test1@gmail.com
This person has the role of an instructor: 1
This person has the role of a contact: 0
Phone: 1234567890
Fax: 1234567890
---------------------PERSONEL-----------------------
First Name: Person2
Last Name: Last2
Email: test2@gmail.com
This person has the role of an instructor: 0
This person has the role of a contact: 1
Phone: 1234567890
Fax: 1234567890

目前显示:

-------------------COURSES_OFFERED------------------
Course Name: Biology
Start Date: 2013-11-13
End Date: 2013-11-26
---------------------PERSONEL-----------------------
First Name: Person1
Last Name: Last1
Email: test1@gmail.com
This person has the role of an instructor: 1
This person has the role of a contact: 
Phone: 1234567890
Fax: 1234567890
-------------------COURSES_OFFERED------------------
Course Name: Calculus
Start Date: 2013-11-19
End Date: 2013-11-16
---------------------PERSONEL-----------------------
First Name: Person1
Last Name: Last1
Email: test1@gmail.com
This person has the role of an instructor: 1
This person has the role of a contact: 
Phone: 1234567890
Fax: 1234567890
-------------------COURSES_OFFERED------------------
Course Name: Biology
Start Date: 2013-11-13
End Date: 2013-11-26
---------------------PERSONEL-----------------------
First Name: Person2
Last Name: Last2
Email: test2@gmail.com
This person has the role of an instructor: 0
This person has the role of a contact: 1
Phone: 1234567890
Fax: 1234567890
-------------------COURSES_OFFERED------------------
Course Name: Calculus
Start Date: 2013-11-19
End Date: 2013-11-16
---------------------PERSONEL-----------------------
First Name: Person2
Last Name: Last2
Email: test2@gmail.com
This person has the role of an instructor: 0
This person has the role of a contact: 1
Phone: 1234567890
Fax: 1234567890

表值

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您的意思是阻止重复的行,则可以使用distinct短语。

select distinct [columns...] 
from ...

请注意distinct可以防止您指定列的重复行。