如何在mysql中查询多个表?

时间:2014-01-27 18:48:01

标签: mysql sql

开始表

knowledge_base

knowledge_id
sub_body_part_id
symptom
disease
specialization_id
gender
age
last_update

main_body_part

main_body_part_id
main_body_part
last_update

sub_body_part

sub_body_part_id
sub_body_part
last_update

专业化

specialization_id
specialization
last_update

sub_body_part_has_main_body_part

sub_body_part_id
main_body_part_id

结束表

这是我的查询。这似乎是正确的,但我没有得到main_body_part表,我也不知道如何查询它。对不起,我是这种情况的初学者。请给我同样的解决方案,我很感激。 :)

SELECT sbp.sub_body_part, s.specialization, k.symptom, k.disease, k.gender, k.age
from knowledge_base k
left join sub_body_part sbp on sbp.sub_body_part_id = k.sub_body_part_id
left join specialization s on s.specialization_id = k.specialization_id

这是一个例子:

          Main body   | Sub body |  Disease |  symptom  |  specialization  |   gender   |   age  


           Head          Eyes        Stroke     Confusion      Neurology        Both        All

1 个答案:

答案 0 :(得分:0)

现在您已将映射表从sub添加到main,请尝试:

SELECT mbp.main_body_part, sbp.sub_body_part, s.specialization, k.symptom, k.disease, k.gender, k.age
from knowledge_base k
left join sub_body_part sbp on sbp.sub_body_part_id = k.sub_body_part_id
left join sub_body_part_has_main_body_part s2m on s2m.sub_body_part_id = sbp.sub_body_part_id
left join main_body_part mbp on mbp.main_body_part_id = s2m.main_body_part_id
left join specialization s on s.specialization_id = k.specialization_id