2015-10-23 91 views
0

我有4个表格学生,教育,访问,医疗数据。我正在尝试创建类似高级搜索表单的用户可以输入标准的内容。从学生表我从教育取得学生的姓名,电话,地址等我正在获得团体,部门和创建和大学名称。从访问我试图得到speciallistApointmant可能会有更多的其中之一。表可以使用桥表student_to_visit,student_to_education和medicaldata_to_student从mysql中选择数据时如何避免重复条目?

下面是有关我目前使用

SELECT 
    DISTINCT 
    s.ID, 
    m.vision, 
    m.hearing, 
    m.movement, 
    m.cardno, 
    v.speciallistApointmant, 
    s.fio, 
    s.birthdate, 
    edge, 
    gender, 
    homeaddress, 
    actualaddress, 
    phone, 
    workplace, 
    enterence, 
    financesource, 
    studyform, 
    c.gruppa, 
    c.greate, 
    c.departmant 
FROM 
    student s, 
    education_to_student b, 
    education c, 
    student_to_visit sv, 
    visits v, 
    medicaldata_to_student ms, 
    medicaldata m 
where 
    b.student_id = s.id 
    and b.education_id = c.id 
    and ms.student_id=s.id 
    and ms.medical_id=m.id 
    and sv.student_id=s.id 
    and sv.visit_id=v.id 
    and gender = 1 

由于因为一个学生可以参观一个以上的专科生SQL查询中显示更多然后一次。此外,如果没有特定学生的访问,该学生不会出现在查询结果中。在上面的例子中,我尝试选择性别= 1的学生,但有更多参数的可能性。如何正确构建这个查询?

这里是PHP代码的形式:

public function getstats($gender,$greate,$edge,$financesource,$birthdate,$hearing,$vision,$movement,$cardno,$speciallistApointmant) 
    { 
     //where begin 

     $where=" where b.student_id = s.id and b.education_id = c.id and ms.student_id=s.id and ms.medical_id=m.id and sv.student_id=s.id and sv.visit_id=v.id"; 
    // echo var_dump($gender); 
     if ($gender!='') 
     { 
      echo var_dump($gender); 
      $where .=" and gender = $gender"; 
    // echo var_dump($gender); 
     } 
      if (!empty($greate)) 
      { 
       $where .="and greate='$greate'"; 
     } 
    if (!empty($edge)) 
    { 
     $where .="and edge='$edge' "; 
    } 
    if (!empty($financesource)) 
    { 
     $where .="and financesource='$financesource'"; 
    } 
    //birthdate add here 


    //end 
    if (!empty($hearing)) 
    { 
     $where.="and hearing='$hearing"; 
    } 
    if (!empty($vision)) 
    { 
     $where .="and vision='$vision'"; 
    } 
     if (!empty($movement)) 
     { 
      $where .="and movement='$movement'"; 
     } 
     if (!empty($cardno)) 
     { 
      $where.="and cardno='$cardno'"; 
     } 
     if (!empty($speciallistApointmant)) 
     { 
      $where .="and speciallistApointmant='$speciallistApointmant'"; 
     } 
     //echo var_dump($where); 
     //where end 

     $sql = "SELECT DISTINCT s.ID, m.vision, m.hearing, m.movement, m.cardno,v.speciallistApointmant, s.fio, s.birthdate, edge, gender, homeaddress, actualaddress, phone, workplace, enterence, financesource, studyform, c.gruppa, c.greate, c.departmant\n" 
    . "FROM student s, education_to_student b, education c,student_to_visit sv, visits v,medicaldata_to_student ms, medicaldata m"."$where"; 
+0

请始终花时间对代码进行格式化,使其一目了然,并且滚动最小 - 特别是水平。 –

+0

如果学生访问多名专家,但每个学生只想返回一行,您希望如何决定回访哪一位专科医生? –

+0

基本上我只返回在PHP表单中指定的specillist其他我甚至不需要选择一个 –

回答

1

SELECT DISTINCT用途上市,以确定是否行不同的或不是所有的领域,因为你用所有的教育,参观,medicaldata领域在你的选择,你会得到不同访问次数的重复学生(或其他表格类似)。

如果您需要查询中的其他表数据(而不仅仅是学生数据),请查看GROUP BY并为其他表字段使用聚合函数(count,min,max等)。

如果您只需要学生列表,请从SELECT语句中删除其他表格列。即使它们不在SELECT语句中,也可以在WHERE语句中使用列。

另外:你真的应该使用PDO(或其他数据库层)和准备你的查询来防止SQL注入攻击。

0

此外,如果没有特定学生的访问,该学生没有出现在查询结果中。

您可以使用LEFT JOIN右加入检索孤儿,这是不相关的另一个表中的记录数据。例如

SELECT s.ID, s.name, m.vision, m.hearing FROM Student s LEFT JOIN medicaldata_to_student ms on s.id=md.student_id LEFT JOIN medicaldata m on ms.medical_id=m.id; 

将检索学生数据与相关的医疗数据。如果学生没有医疗数据,他的ID和他的名字将被设置,并且医疗数据将被设置为空。