ThinkPHP5多表查询

原生多表查询

使用规则

创建公共类,实例化调用,或者写成静态方法,都是可以的。

<?php
namespace app\common\lib;
use think\Controller;
use think\Db;
class Commonsql extends Controller
{
    /**
     * @param $tid
     * @param $status
     * @return array $data 名师
     */
    public function get_teacher_msg($tid,$status)
    {
        $_sql = "SELECT bd_teacher.id,bd_teacher.name,bd_teacher.face,bd_teacher.tip,bd_teacher.introduce,bd_teacher.status,bd_teacher_title.t_name,bd_major.m_name
                        FROM bd_teacher,bd_teacher_title,bd_major 
                            WHERE bd_teacher.title = bd_teacher_title.id
                                AND bd_teacher.major = bd_major.id
                                    AND bd_teacher.status = $status
                                        AND bd_teacher.id = $tid";
        $ret = Db::query($_sql);
        return $ret;
    }
}

TP5自带方法

使用前提

避免重复调用,并且可以使用框架方法

$count = Model('Teacher')
                    ->join('bd_teacher_title ',' bd_teacher.title = bd_teacher_title.id')
                    ->join('bd_major ',' bd_teacher.major = bd_major.id')
                    ->field('bd_teacher.id,bd_teacher.name,bd_teacher.face,bd_teacher.tip,bd_teacher.introduce,bd_teacher.status,bd_teacher_title.t_name,bd_major.m_name')
                    ->page($NowPage,$limits)
                    ->where($map)
                    ->count();
$lists = Model('Teacher')
                    ->join('bd_teacher_title ',' bd_teacher.title = bd_teacher_title.id')
                    ->join('bd_major ',' bd_teacher.major = bd_major.id')
                    ->field('bd_teacher.id,bd_teacher.name,bd_teacher.face,bd_teacher.tip,bd_teacher.introduce,bd_teacher.status,bd_teacher_title.t_name,bd_major.m_name')
                    ->page($NowPage,$limits)
                    ->where($map)
                    ->select();
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读