W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
查詢構(gòu)造器也可以使用 join 語法,看看下面的例子:
DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.id', 'contacts.phone', 'orders.price')
->get();
DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->get();
你也可以指定更高級的 join 子句:
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
})
->get();
如果你想在你的 join 中使用 where 型式的子句,你可以在 join 子句里使用 where 或 orWhere 方法。下面的方法將會(huì)比較 contacts 數(shù)據(jù)表中的 user_id 的數(shù)值,而不是比較兩個(gè)字段。
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: