【问题标题】:Avoid Duplicate values displaying from select statement避免从 select 语句中显示重复值
【发布时间】:2013-11-23 17:08:44
【问题描述】:

我正在使用 foreach 循环来获取结果,但我得到了显示不必要的重复结果。下面你会看到我的值出现了两次以上。在我的查询选择语句中,我在表之间进行内部连接。然后我做一个foreach 循环来显示这些结果。结果显示在三个表中:academy、courses_by_academy 和 person。他们都共享一个外键 academy_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

表值

【问题讨论】:

  • 你为什么说你有重复?您在图片中显示的四行都是不同的。

标签: php mysql


【解决方案1】:

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

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

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

【讨论】:

  • 它适用于完全相似的行。在您的示例中,四行不同且不重复。
  • 如何显示我想要的信息(想要的显示方式)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多