Thinkphp5 的經常使用連式查詢


取出表中改字符串前兩位等於01的數據

$pepper_count = $this->orderModel->where("instr(order_id,'02')",2)->count('id');

按主鍵查詢

按主鍵查用放在括號裏thinkphp

//把按商戶和平臺的id查出來的數據傳到edit前臺模板裏
$id=input('id');
$agencyRes=db('merchant_agency')
    ->field('a.*,b.agency_name')
    ->alias('a')
    ->join('agency b','a.agency_id=b.id')
    ->find($id);

不按主鍵查

$userInfo=db('user')

        ->field('a.*,b.group_id,c.title')

        ->alias('a')

        ->join('role_group_access b','a.uid=b.uid')

        ->join('role_group c','b.group_id=c.group_id')

        ->where('a.uid',$uid)

        ->find();

JOIN方法 的左右鏈接

JOIN方法也是連貫操做方法之一,用於根據兩個或多個表中的列之間的關係,從這些表中查詢數據。thinkphp5

$goods_list =  db('goods_menu_dishes')
            ->alias('g')
            ->join('agency_goods d',"g.menu_id = d.menu_id ",'left') //關聯類型。能夠爲:INNER、LEFT、RIGHT、FULL,不區分大小寫,默認爲INNER。
            ->where("g.merchant_id ",$merchant_id)
            ->where('d.agency_id',$agency_id)
            ->where('g.menu_id',$menu_id)
            ->field("g.goods_sn,d.out_sku_id")
            ->select();
        var_dump($goods_list);
        die;

not in 方法

$authGroupRes=db('role_group')
          ->field('group_id,title')
          ->where('group_id','not in','1,3,4')
          ->select();

like 查詢

use think\Db;  //上面須要use
//取出廣告欄裏title裏含有index_banner關鍵字而且設置爲推薦的全部輪播圖信息
// $indexModel=new indexModel;
$bannerRes=Db::table('bk_cate')
            ->field('b.*,a.catename')
            ->alias('a')
            ->join('bk_article b','a.id = b.cateid')
            ->where('catename','like','%輪播圖%')
            ->where('rec','eq',1)
            ->limit(5)
            ->select();

where 按條件篩選查詢

if (isset($data['station_name']) && empty($data['station_name'])) {
                unset($data['station_name']);
            } else {
                $where['a.station_name'] = ['like', "%" . $data['station_name'] . "%"];
            }
            if (isset($data['erp_code']) && empty($data['erp_code'])) {
                unset($data['erp_code']);
            } else {
                $where['a.erp_code'] = ['=', $data['erp_code']];
            }
            if (isset($data['city']) && $data['city'] == '[2,25]') {
                $where['a.city'] = ['not in', $data['city']];
            } else {
                $where['a.city'] = ['=', $data['city']];
            }

            if (isset($where['a.erp_code']) && empty($where['a.erp_code'])) {
                unset($where['a.station_name']);
            }

            $data = $this->seachAgencyStationiInfo($where);
public function seachAgencyStationiInfo($where = '')
    {
        //查出全部平臺的信息展現出來,供開通平臺選用
        $agencyInfo = db('agency')->where('display','=',1)->select();
        $this->assign('agencyInfo', $agencyInfo);

        //查出表格的前半部分顯示數據
        $shopAgencyInfo = db('shop_station')
            ->field('a.station_id,a.merchant_id,a.daqu_id,a.erp_code,a.city,a.station_name,a.tel,b.region_name')
            ->alias('a')
            ->join('region b', 'a.city=b.region_id')
            ->where(isset($where) ? $where : '')
            // ->fetchSql()
            // ->order('station_id desc')
            ->order('city asc')
            ->paginate(20);
   }

更詳情點下面鏈接
JOIN更多操做fetch

相關文章
相關標籤/搜索