在MySQL上的四个表上的内连接

时间:2014-08-05 21:59:32

标签: mysql join

我正在尝试在MySQL中的四个表上进行内部联接。表名是:

question (id_question, question_text, id_standard)
standard (id_standard, standardtext)
organisation_standard(id_organisation,id_organisation,id_standard)
organisation (id_organisation, organisation_name)

这是我的查询,它给了我重复的价值观:

select distinct a.question_text, d.organisation_name
from question a
inner join standard b on a.id_standard = b.id_standard
inner join organisation_standard c on b.id_standard= c.id_standard
inner join organisation d on c.id_organisation = d.id_organisation
where a.id_standard = 18;

如何避免重复值?

1 个答案:

答案 0 :(得分:-1)

你需要的是left join而不是内连接将内连接更改为左连接,你将只得到一行:

select distinct
    a.question_text, d.organisation_name
from
    question a
        left join
    standard b ON a.id_standard = b.id_standard
        left join
    organisation_standard c ON b.id_standard = c.id_standard
        left join
    organisation d ON c.id_organisation = d.id_organisation
where
    a.id_standard = 18
group by a.id_standard;

来自another so answer的图表给出了不同联接之间的区别 enter image description here