溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ThinkPHP多表聯(lián)合查詢的常用方法有哪些

發(fā)布時間:2021-07-01 11:39:44 來源:億速云 閱讀:264 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“ThinkPHP多表聯(lián)合查詢的常用方法有哪些”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“ThinkPHP多表聯(lián)合查詢的常用方法有哪些”吧!

ThinkPHP中關(guān)聯(lián)查詢(即多表聯(lián)合查詢)可以使用 table() 方法或和join方法,具體使用如下例所示:

1、原生查詢示例:

$Model = new Model();
$sql = 'select a.id,a.title,b.content from think_test1 as a, think_test2 as b where a.id=b.id '.$map.' order by a.id '.$sort.' limit '.$p->firstRow.','.$p->listRows;
$voList = $Model->query($sql);

2、join()方法示例:

$user = new Model('user');
$list = $user->join('RIGHT JOIN user_profile ON user_stats.id = user_profile.typeid' );

Thinkphp使用join聯(lián)表查詢的方法

$user = M('user');
$b_user = M('b_user');
$c_user = M('c_user');
$list = $user->alias('user')->where('user.user_type=1')
  ->join('b_user as b on b.b_userid = user.user_id')
  ->join('c_user as c on c.c_userid = b.b_userid')
  ->order('b.user_time')
  ->select();

$user 表的 user_id 等于$b_user表的b_userid;

$c_user表的 c_userid 等于$b_user表的b_userid;

3、table()方法示例:

$list = $user->table('user_status stats, user_profile profile')->where('stats.id = profile.typeid')->field('stats.id as id, stats.display as display, profile.title as title,profile.content as content')->order('stats.id desc' )->select();

到此,相信大家對“ThinkPHP多表聯(lián)合查詢的常用方法有哪些”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI