简体   繁体   English

左连接以在 Laravel 中获得一行

[英]Left join to get a single row in Laravel

I have been unsuccessfully trying to leftjoin and get the required data我一直没有成功尝试 leftjoin 并获取所需的数据

Here is my code:这是我的代码:

$album = Albums::->where('users_id',$user_id)
           ->leftJoin('photos',function($query){
              $query->on('photos.albums_id','=','albums.id');
              $query->where('photos.status','=',1);     
                //$query->limit(1);
                //$query->min('photos.created_at');
              })
           ->where('albums.status',1)->get();

The comments are some of my several trying...这些评论是我的一些尝试...

I want to get only a single record from the photos table matching the foreign key album_id which was updated first and also with status 1我想从照片表匹配外键只有一个记录album_id这是第一次,也更新了状态1

pls help...请帮助...

I have used DB::raw() in order to achieve this我使用DB::raw()来实现这一点

$album  =   Albums::select( 'albums.*',
            DB::raw('(select photo from photos where albums_id  =   albums.id  and status = 1 order by id asc limit 1) as photo')  )
            ->where('users_id',$user_id)
            ->where('albums.status',1)->get();

@JarekTkaczyk 's coding was similar and displayed the same result as I needed, so a special thanks to him for his time and effort... @JarekTkaczyk 的编码与我需要的相似并显示出相同的结果,所以特别感谢他的时间和努力......

But comparing the execution time for the quires I stayed to mine as my above snippet但是比较我留下来的quires的执行时间作为我上面的片段

select `albums`.*, (select photo from photos where albums_id    =   albums.id  and status = 1 order by id asc limit 1) as photo from `albums` where `users_id` = '1' and `albums`.`status` = '1'

took 520μs - 580μs花了520μs - 580μs

and @JarekTkaczyk 's和 @JarekTkaczyk 的

select `albums`.*, `p`.`photo` from `albums` left join `photos` as `p` on `p`.`albums_id` = `albums`.`id` and `p`.`created_at` = (select min(created_at) from photos where albums_id = p.albums_id) and `p`.`status` = '1' where `users_id` = '1' and `albums`.`status` = '1' group by `albums`.`id`

took 640μs - 750μs But both did the same...花了640μs - 750μs但两者都做了同样的事情......

You can achieve it using either leftJoin or rightJoin (but the latter would return Photo models, so probably you won't need that):您可以使用leftJoinrightJoin来实现它(但后者会返回Photo模型,因此您可能不需要它):

Albums::where('users_id', $user_id)
 ->leftJoin('photos as p', function ($q) {
   $q->on('photos.albums_id', '=', 'albums.id')
     ->on('photos.updated_at', '=', 
       DB::raw('(select min(updated_at) from photos where albums_id = p.albums_id)'))
     ->where('photos.status', '=', 1);
 })
 ->where('albums.status', 1)
 ->groupBy('albums.id')
 ->select('albums.*', fields from photos table that you need )
 ->get();

Are you trying to check for albums that have the status of '1'?您是否要检查状态为“1”的专辑? If this is the case you are missing an equals sign from your final where.如果是这种情况,您将缺少最终 where 中的等号。

Try:尝试:

 ->where('albums.status','=',1)->first();

Alternatively you may be able to achieve this with an 'on' instead of a 'where' inside the join function.或者,您可以在 join 函数中使用“on”而不是“where”来实现这一点。 You also don't need to split up query inside of the function and can do it as one line with the '->' :您也不需要在函数内部拆分查询,可以将其作为一行与 '->' 一起进行:

$album  =   Albums::->where('users_id',$user_id)
                    ->leftJoin('photos',function($query){
                            $query->on('photos.albums_id','=','albums.id')
                            ->on('photos.status','=',1);
                    })
        ->where('albums.status','=',1)->first();

You need to make sure that you are using 'first', as it will return a single row of the first result.您需要确保使用的是“first”,因为它将返回第一个结果的单行。 Get() will return an array. Get() 将返回一个数组。

As a straightforward answer which results in a single object I suggest the following query:作为导致单个对象的直接答案,我建议使用以下查询:

$album = DB::table('albums')->select('albums.*', 'photos.photo')
    ->join('photos', 'photos.id', '=', 'albums.id')
    ->where('users_id',$user_id)
    ->where('albums.status',1)
    ->first();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM