是否有可能在Laravel 5中执行此查询?
select t1.*, v.hash from
( select dr.*, d.id as d_id, d.name, d.user_id from directory d
left join directory_relations dr on d.id = dr.directory_id
union select dr.*, d.id as d_id, d.name, d.user_id from directory_relations dr
left join directory d on d.id = dr.directory_id ) t1
left join videos v on t1.video_id = v.id WHERE t1.user_id = 265
答案 0 :(得分:1)
您可以使用DB :: raw
$results = DB::select( DB::raw("select t1.*, v.hash from
( select dr.*, d.id as d_id, d.name, d.user_id from directory d
left join directory_relations dr on d.id = dr.directory_id
union select dr.*, d.id as d_id, d.name, d.user_id from directory_relations dr
left join directory d on d.id = dr.directory_id ) t1
left join videos v on t1.video_id = v.id WHERE t1.user_id = :user_id"), array(
'user_id' => 265,
));
答案 1 :(得分:0)
使用查询构建器,您可以这样做:
$union = DB::select(['dr.*', 'd.id as d_id', 'd.name', 'd.user_id'])
->from('directory_relations as dr')
->leftJoin('directory as d', 'd.id', '=', 'dr.directory_id');
$base = DB::select(['dr.*', 'd.id as d_id', 'd.name', 'd.user_id'])
->from('directory as d')
->leftJoin('directory_relations as dr', 'd.id', '=', 'dr.directory_id')
->union($union);
DB::select('t1.*', 'v.hash')
->from(DB::raw("({$base->toSql()}) as t1"))
->mergeBindings($base->getQuery())
->leftJoin('videos as v', 't1.video_id', '=', 'v.id')
->where('t1.user_id', 265)
->get();