1

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

    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

4

2 回答 2

1

使用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
于 2013-07-06T14:48:41.013 回答
0

尝试这个..

select * from #__unis_faculties AS faculty
join #__unis_subjects AS subject ON subject.faculty = faculty.id
left join #__unis AS uni ON  ( uni.id= subject.university AND uni.id = 1 )
于 2013-07-06T18:43:27.230 回答