我有以下查询,只有连接存在时才返回数据。如何从最后一个连接的表(##ú;uUnis)数据返回,即使这些表之间没有关系,也不必编写另一个查询?

    select * from #__unis_faculties AS faculty
    join #__unis_subjects AS subject ON subject.faculty = faculty.id
    join #__unis AS uni ON  uni.id= subject.university
    where uni.id = 1

表结构http://sqlfiddle.com/#!2/19add

最佳答案

使用LEFT JOIN而不是join

   select * from #__unis_faculties AS faculty
 join #__unis_subjects AS subject ON subject.faculty = faculty.id
 right join #__unis AS uni ON  uni.id= subject.university
 where uni.id = 1

关于mysql - 联合查询以返回数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17504017/

10-16 03:50