MySQL多个条件关联,join多个条件关联,on后面大于小于><
介绍
使用join关联表时on后面写多个条件
代码
SELECT
*
FROM
lecture l
INNER JOIN lecture_limit lt ON l.id = lt.lecture_id
INNER JOIN student_class sc ON sc.CODE = lt.CODE
OR floor( sc.CODE / 1000 ) = lt.CODE
OR floor( sc.CODE / 1000000 ) = lt.CODE
OR floor( sc.CODE / 100000000 ) = lt.CODE
OR floor( sc.CODE / 100000000000 ) = lt.CODE
OR floor( sc.CODE / 1000000000000 ) = lt.CODE
where sc.teacher_id =3
thinkphp组装
$res = $lecture
->field('*')
->alias('l')
->join('lecture_limit lt', 'l.id = lt.lecture.id')
->join('student_class sc', 'sc.code = lt.code or floor(sc.code / 1000) = lt.code or floor(sc.code / 1000000) = lt.code or floor(sc.code / 100000000) = lt.code or floor(sc.code / 100000000000) = lt.code or floor(sc.code / 1000000000000) = lt.code')
->where($where)
->order(['l.id' => 'desc'])
->paginate([
'list_rows' => $size,
'page' => $page,
]);
效果