使用laravel连接多个表

时间:2015-01-26 13:56:44

标签: php mysql laravel laravel-4

您好我正在尝试使用laravel中的join进行以下查询。

select  advertisers.advertiserName,brands.brandName  
        ,campaigns.campaignName,banner_creatives.bannerName         
from vicinity_adman_dev.advertisers
     join vicinity_adman_dev.brands 
     join vicinity_adman_dev.campaigns
     join vicinity_adman_dev.banner_creatives

     on advertisers. advertiserId = brands.advertiserId
     where 
     banner_creatives.campaignId = campaigns.campaignId

到目前为止我所说的:

  $campaigns =DB::table('brands')

 ->join('advertisers','brands.advertiserId' , '=','advertisers.advertiserId'               
                ->join('campaigns','banner_creatives.campaignId',
                        '=','campaigns.campaignId')  

  select('advertisers.advertiserName','brands.brandName','campaigns.campaignName','banner_creatives.bannerName')   
->get();

第二次加入时收到错误“ banner_creatives.campaignId ” 提前谢谢

1 个答案:

答案 0 :(得分:0)

你错过了一个联盟。只需将其添加到其他人

即可
$campaigns = DB::table('brands')
               ->join('advertisers', 'brands.advertiserId', '=', 'advertisers.advertiserId')
               ->join('campaigns', 'banner_creatives.campaignId', '=', 'campaigns.campaignId')
               ->join('banner_creatives', 'banner_creatives.bannerCreativeId', '=', 'other_table.bannerCreativeId')
               ->select(
                   'advertisers.advertiserName',
                   'brands.brandName',
                   'campaigns.campaignName',
                   'banner_creatives.bannerName'
               )
               ->get();