加入查询以返回数据

时间:2013-07-06 14:47:08

标签: mysql join

我有以下查询,只有在连接存在时才返回数据。如何返回上一个连接表(#__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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 )