dphs48626 2015-06-11 14:37
浏览 39
已采纳

Laravel 2表连接左表中的所有表与右表的成员合并,具有给定的FK值

I have two tables in Laravel of which I am seeking to merge them together, however, I want to return every single value of the first table (without duplicates) along with only values from the second table that have a FK value of 2. If there is no entry with a FK of 2, it joins with a value of null.

To make my question a little more clear, lets say we have the following tables:

TV Shows Table
ID  |  Show
1   |  First Show
2   |  Second Show
3   |  Third Show

Favorites Table
Show_ID  |  Member_ID
1        |  1 
3        |  1 
1        |  2 
2        |  2

I am looking to merge them into a resultant set like the following when I join the tables with a member ID of 2(disregarding the joined 'Show_ID' column):

Merged Table
ID  |  Show         |  Member_ID
1   |  First Show   |  2
2   |  Second Show  |  2
3   |  Third Show   |  null

Thanks.

  • 写回答

2条回答 默认 最新

  • dongyu9667 2015-06-11 15:14
    关注

    I solved it myself. I needed to do a functional join like so:

    DB::table('shows')
        ->leftJoin('favorites', function($q) use $memberID)
        {
            $q->on('shows.ID', '=', 'favorites.show_ID')
                ->where('favorites.member_ID', '=', $memberID);
        })->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动