【问题标题】:Filter row of table-1 on the basis of table-2 column value , no attachement between table-1 and table -2根据table-2的列值过滤table-1的行,table-1和table-2之间没有附件
【发布时间】:2016-08-02 12:48:41
【问题描述】:

我有 7 张这样的桌子

诊所

  clinic_id     clinic_name    emailId              address

诊所和护士桌之间的一对一映射

护士

  nurse_id  nurse_name  nurse_emailid      clinic_id           

医生

    doctor_id    doctor_name   doctor_email  

诊所和医生之间的多对多映射

医生诊所

    doctor_clinic_id  doctor_id   clinic_id

病人

   pattient_id   patient_name     address

patinet 和医生诊所之间的多对多映射

医生_诊所_病人

    patient_dotor_id   doctor_clinic_id  patient_id

医生_诊所_病人和医生_病人预约之间的一对多映射

医生_病人_预约

     appointment_id  appointment_time  appointment_status patient_doctor_id

appointment_status 有三个值-'pending','cancelled','completed'

所以,senario 从这里开始

  1. 从nurse_id(护士表)中选择clinic_id

  2. 那么在 Clinic_id 的基础上,我要选择医生_clinic_id(医生诊所表)

  3. 那么根据doctor_clinic_id,我要选择patient_id

  4. 然后在patient_id的基础上,我想从患者表中选择患者详细信息

问题是,我只想从病人表中选择那些在状态为“待定”的预约表中有预约的病人

我做了以下工作正常的查询,从数据库中获取患者行。但我想根据预约状态过滤这个患者结果

查询

  Select * from patient where patient.patient_id in 

 (select doctor_patient.patient_id from doctor_patient where 

 doctor_patient.doctor_clinic_id in
 (select doctor_clinic.doctor_clinic_id from doctor_clinic where  

 doctor_clinic.clinic_id  in 
 (select nurse.clinic_id from nurse where nurse_id = 6) ))

【问题讨论】:

  • 这是一个复杂的模式。你有没有尝试过?
  • 是的,我进行了查找患者详细信息的查询,但我不知道如何根据约会状态进行过滤
  • @TimBiegeleisen。我发布了查询。请提出任何建议。

标签: mysql


【解决方案1】:

我想这里可以使用多个 JOIN:

SELECT patients.* FROM patients JOIN (
  SELECT DISTINCT(p.patient_id) as pat_id FROM clinic c 
  JOIN doctor_clinic dc ON c.clinic_id = dc.clinic_id
  JOIN doctor_clinic_patient dcp ON dcp.doctor_clinic_id = dc.doctor_clinic_id
  JOIN patient p ON p.patient_id = dcp.patient_id
  JOIN doctor_patient_appointment dpa ON dcp.patient_doctor_id = dpa.patient_doctor_id
  WHERE dpa.status = 'pending'
) tmp ON tmp.pat_id = patients.patient_id

【讨论】:

  • 我在问题中发布了查询,您能看到吗?如果需要,请提供任何建议
  • 你试过我的答案了吗?它似乎适合您的需求。顺便说一句:我不太明白你的问题,因为它过滤掉了护士的结果,而不是预约(你问的)......
  • 是的,你是对的,但我如何应用那个约会过滤器我的查询。非常感谢您的快速回复,并为这种复杂的事情提供解决方案。
  • 还有一件事,如果我想从另一个表中获取列,例如从约会表中获取某些列,从 patient_doctor 表中获取某些列,那么我必须在您的查询中进行哪些更改。
  • 只需删除外部查询并在内部查询中的SELECT 语句中添加适当的字段 - 即将 `DISTINCT(p.patient_id) 替换为 doctor_patient_appointment.* 以获取所有预约
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 2021-10-07
  • 2020-09-17
相关资源
最近更新 更多