SQL查询需要大量时间来响应

时间:2016-04-29 10:07:51

标签: mysql sql-server query-builder

我有一个像

这样的SQL查询
SELECT c.Case_Id, c.Disease_Sub_Type_Id, p.MedRecNo 
FROM case1 c, patient p, episode e, personal_medical_history pmh, reproductive_history rh 
WHERE c.Age <> '12' AND c.horizontal_spread <> '21' AND c.Patient_Id = p.Patient_Id 
      AND e.Case_Id = c.Case_Id AND pmh.Patient_Id = p.Patient_Id 
      AND rh.Patient_Id = p.Patient_Id 
GROUP BY c.Case_Id`

当我使用上面的查询时,这给了我很快的结果,但当我使用OR作为条件时

SELECT c.Case_Id, c.Disease_Sub_Type_Id, p.MedRecNo 
FROM case1 c, patient p, episode e, personal_medical_history pmh, reproductive_history rh 
WHERE c.Age <> '12' OR c.horizontal_spread <> '21' AND c.Patient_Id = p.Patient_Id AND e.Case_Id = c.Case_Id AND pmh.Patient_Id = p.Patient_Id 
      AND rh.Patient_Id = p.Patient_Id 
GROUP BY c.Case_Id`

我没有得到输出而且MySQL没有挂起。是否有任何简单的方法来应用加入或我的方法是错误的?

1 个答案:

答案 0 :(得分:0)

尝试将OR包含在这样的括号中;

SELECT c.Case_Id
    ,c.Disease_Sub_Type_Id
    ,p.MedRecNo
FROM case1 c
    ,patient p
    ,episode e
    ,personal_medical_history pmh
    ,reproductive_history rh
WHERE (c.Age <> '12'
    OR c.horizontal_spread <> '21')
    AND c.Patient_Id = p.Patient_Id
    AND e.Case_Id = c.Case_Id
    AND pmh.Patient_Id = p.Patient_Id
    AND rh.Patient_Id = p.Patient_Id
GROUP BY c.Case_Id`