如何从外键链接的两个不同表中查询

时间:2013-02-20 12:20:25

标签: mysql sql select join

heres a table of diagnosis i want to query the diagnosis and count the number of patient_ID diagnosed by it. so it has to be one diagnoes with numbers of patient BUT the next picture comes in the complaint table part where the query has to be with a certain month only

enter image description here

下面是诊断表我想查询诊断并计算由其诊断的patient_ID的数量。所以它必须是一个有患者数量的诊断但是下一张图片出现在投诉表部分,其中查询必须只有某个月

1 个答案:

答案 0 :(得分:1)

我猜您需要将两个表连接到两列:complaint_IDpatient_ID

SELECT  a.diagnosis, COUNT(a.patient_ID) totalAmidtted
FROM    diagnosis a
        LEFT JOIN admitted b
            ON  a.complaint_ID = b.complaint_ID AND
                a.patient_ID = b.patient_ID
WHERE   b.date_time BETWEEN 'fromDate' AND 'toDate'
GROUP   BY a.diagnosis

要进一步了解联接,请访问以下链接:

偏离主题:你是菲律宾人吗?特别是bisaya :D