我有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";
请始终花时间对代码进行格式化,使其一目了然,并且滚动最小 - 特别是水平。 –
如果学生访问多名专家,但每个学生只想返回一行,您希望如何决定回访哪一位专科医生? –
基本上我只返回在PHP表单中指定的specillist其他我甚至不需要选择一个 –