溫馨提示×

溫馨提示×

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

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

thinkphp如何查詢多個數(shù)據(jù)

發(fā)布時間:2022-12-05 09:33:22 來源:億速云 閱讀:100 作者:iii 欄目:編程語言

這篇文章主要介紹“thinkphp如何查詢多個數(shù)據(jù)”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“thinkphp如何查詢多個數(shù)據(jù)”文章能幫助大家解決問題。

thinkphp查詢多個數(shù)據(jù)的方法:1、使用Table方法進行多表查詢,語法如“$Model->table('think_blog blog,think_type type')”;2、使用Join方法進行查詢,代碼如“$Model->join('work ON artist.id = work.artist_id')”。

THINKPHP 中關(guān)聯(lián)查詢(多表查詢)

THINKPHP 中關(guān)聯(lián)查詢(多表查詢)可以使用 table() 方法或和join方法,請看示例:

1、Table方法:定義要操作的數(shù)據(jù)表名稱,可以動態(tài)改變當前操作的數(shù)據(jù)表名稱,需要寫數(shù)據(jù)表的全名,包含前綴,可以使用別名,例如:

$Model->Table('think_user user')
->where('status>1')
->select();
$Model->table('think_blog blog,think_type type')
->where('blog.typeid=type.id')
->field('blog.id as id,blog.title,blog.content,type.typename as type')
->order('blog.id desc' )
->limit(5)
->select();

Table方法的參數(shù)支持字符串和數(shù)組,數(shù)組方式的用法:

$Model->Table(array('think_user'=>'user','think_group'=>'group'))
->where('status>1')
->select();

使用數(shù)組方式定義的優(yōu)勢是可以避免因為表名和關(guān)鍵字沖突而出錯的情況。

注:如果不定義table方法,默認會自動獲取當前模型對應(yīng)或者定義的數(shù)據(jù)表。

2、Join方法:查詢Join支持,Join方法的參數(shù)支持字符串和數(shù)組,并且join方法是連貫操作中唯一可以多次調(diào)用的方法。例如:

$Model->join('work ON artist.id = work.artist_id')
->join('card ON artist.card_id = card.id')
->select();
//Left Join
$Model->table('user U')
->join('news N on U.id=N.cid')
->field('U.*,N.*')
->order('id desc')
->limit('8')
->findall();

默認采用LEFT JOIN 方式,如果需要用其他的JOIN方式,可以改成

$Model->join('RIGHT JOIN work ON artist.id = work.artist_id')
->select();
//Right Join
$Model->table('user U')
->join(array('right','news N on U.id=N.cid'))
->field('U.*,N.*')
->order('id desc')
->limit('8')
->findall();

如果join方法的參數(shù)用數(shù)組的話,只能使用一次join方法,并且不能和字符串方式混合使用。

$Model->join(array(' work ON artist.id = work.artist_id', 'card ON artist.card_id = card.id'))
->select()

運用這種連貫操作方法,可以有效的提高數(shù)據(jù)查詢的代碼清晰度和開發(fā)效率。

查看連貫操作的SQL語句的方法:

echo $Model->getLastSql(); //打印一下SQL語句,查看一下

例2:

1、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();

2.1、join()2表查詢

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

2.2、join() 多表查詢

        $list = $Form->join('think_sort ON think_form.sort_id = think_sort.sort_id' )->join('think_brand ON think_form.brand_id = think_brand.brand_id' )->select();

3、原生查詢

$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);

關(guān)于“thinkphp如何查詢多個數(shù)據(jù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向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