我有两个来自两个不同表的查询。第一个包含我要在第二个查询中使用的ID。现在,我这样做。我将ID提取到新数组中,然后->whereIn()
在第二个查询中使用此数组。
$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
?
您需要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();
希望你能理解
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句