废话不多说,也不截图了。。直接来SQL语句
select * from major_subject a join (select subject_no from major_subject_classes a join (select class from fa_user where id = '1') b on a.class = b.class) b on a.subject_no = b.subject_no
这是 fa_user、major_subject_classes、major_subject 三表的关联
major_subject_classes 与 major_subject 通过class字段关联
fa_user 与 major_subject_classes 通过字段subject_no字段关联
关联的代码:
$userSql = Db::table('fa_user')->field('class')->where('id',$userId)->buildSql();
$classSql = Db::table('major_subject_classes a')->field('subject_no')->join($userSql.'b','a.class = b.class')->buildSql();
$subjectList = Db::table('major_subject a')->join($classSql.'b','a.subject_no = b.subject_no')->order('major')->select();
$userId是查询条件,思想就是,先通过buildSql()来构建语句,最后再进行查询。
这是我实现的三表关联。。。
通过这种方式,可以实现更多个表的关联。