2017-07-24 20 views
2

我有两个查询来自两个不同的表。第一个包含我想在第二个查询中使用的ID。现在我这样做。我将ID提取到一个新数组中,然后在第二个查询中使用->whereIn()中的该数组。如何将ID数组从一个Laravel查询传递到另一个

$campaign = DB::connection('mysql2')->table('mc_spots') 
      ->select('s_customer', 's_product', 'spotid') 
      ->where('s_starttermin', '>=', date("Y-m-d", strtotime($dateFrom))) 
      ->where('s_lastrun', '<=', date("Y-m-d", strtotime($dateUntil))) 
      ->where('s_media', '=', $media) 
      ->where(function ($query) use ($products) { 
       for ($i = 0; $i < count($products); $i++) { 
        $query->orwhere('s_product', '=', $products[$i]); 
       } 
      }) 

      ->get(); 

$campaignID = []; 

     foreach ($campaign as $c) { 
      array_push($campaignID, $c->spotid); 
     } 

$table = DB::connection('mysql2')->table('schaltdaten_tv_de') 
       ->select('*') 
       ->join('epgdata_channel', 'schaltdaten_tv_de.cid', '=', 'epgdata_channel.channelid') 
       ->join('mc_spots', 'schaltdaten_tv_de.ReferenceDescription', '=', 'mc_spots.spotid') 
       ->whereIn('ReferenceDescription', $campaignID) 
       ->groupBy('epgdata_2013.id') 
       ->orderBy('StartofBreak', 'ASC') 
       ->limit(500) 
       ->get(); 

有没有更方便的方式来做到这一点没有通过$campaign每个项目的循环?

+1

您的问题中的代码只使用Laravel的流利的查询生成器,而不是雄辩。 –

+0

好的。我编辑了我的问题。 Thx – harunB10

+0

您使用的是什么版本的Laravel? –

回答

1

您需要从第一个查询中摘取id,该查询为您提供了ID数组,您可以在第二个查询中使用它。现在

$campaign = DB::connection('mysql2')->table('mc_spots') 
     ->select('s_customer', 's_product', 'spotid') 
     ->where('s_starttermin', '>=', date("Y-m-d", strtotime($dateFrom))) 
     ->where('s_lastrun', '<=', date("Y-m-d", strtotime($dateUntil))) 
     ->where('s_media', '=', $media) 
     ->where(function ($query) use ($products) { 
      for ($i = 0; $i < count($products); $i++) { 
       $query->orwhere('s_product', '=', $products[$i]); 
      } 
     }) 

     ->pluck('spotid'); 

,使用spot id阵列中的第二个为whereIn

$table = DB::connection('mysql2')->table('schaltdaten_tv_de') 
      ->select('*') 
      ->join('epgdata_channel', 'schaltdaten_tv_de.cid', '=', 'epgdata_channel.channelid') 
      ->join('mc_spots', 'schaltdaten_tv_de.ReferenceDescription', '=', 'mc_spots.spotid') 
      ->whereIn('ReferenceDescription', $campaign) 
      ->groupBy('epgdata_2013.id') 
      ->orderBy('StartofBreak', 'ASC') 
      ->limit(500) 
      ->get(); 

希望你能理解

+0

很酷。是否有可能从第二个查询中提取SUM列?我需要从所有项目中获得'$ table'中的SUM('price')'。现在我做这个例子 - 分配'$ sum = 0',然后遍历所有的项目,并使'$ sum + = $ table-> price' ...这是非常糟糕的解决方案,但... – harunB10

+0

当然可能,你试过' - > sum()'? –

+0

你的意思是'$ sum = $ table-> sum()'?但是,我怎样才能通过'价格'(列的名称)? – harunB10

1

你可以像下面这样做

$campaignID = $campaign->pluck('spotid'); 

普吕克DOC:

摘去方法检索所有的值对于给定的关键

而作为萨格尔说,它检索一个数组,这是我们的第二个参数->whereIn()

+0

'pluck()'已经给出数组,所以你不能再次将它转换为数组 –

+0

没有注意到,因为它没有给出任何错误 – aaron0207

+0

没有错误,所以它是好的,但不必要的 –

1

您可以通过使用array_column的做到这一点()

喜欢这个

$campaignID=array_column($campaign,'spotid') 

确保$campaign必须是数组。如果是它的对象转换成数组这样json_decode(json_encode($campaign),true)

对于exapmle

$t='[{"id":49},{"id":61},{"id":5},{"id":58}]' ; 
array_column(json_decode($t,true),'id') 

它会给输出

enter image description here

0

正如你已经在加入mc_spots表第二个查询,您可以随时添加第一个查询中的相同约束条件:

$table = DB::connection('mysql2')->table('schaltdaten_tv_de') 
    ->select('*') 
    ->join('epgdata_channel', 'schaltdaten_tv_de.cid', '=', 'epgdata_channel.channelid') 
    ->join('mc_spots', 'schaltdaten_tv_de.ReferenceDescription', '=', 'mc_spots.spotid') 
    ->where('mc_spots.s_starttermin', '>=', \Carbon\Carbon::parse($dateFrom)) 
    ->where('mc_spots.s_lastrun', '<=', \Carbon\Carbon::parse($dateUntil)) 
    ->where('mc_spots.s_media', $media) 
    ->whereIn('s_product', $products) 
    ->groupBy('epgdata_2013.id') 
    ->orderBy('StartofBreak', 'ASC') 
    ->limit(500) 
    ->get(); 

希望这有助于!

+0

我正在尝试实现您的解决方案,但该页面现在只是加载。当我进入MySQL进程时,我可以看到它只是继续发送数据。我不得不手动杀死进程。 – harunB10

+0

@ harunB10我为您添加了不同的解决方案。 –

相关问题