dsm42026 2014-05-12 14:52
浏览 21
已采纳

如果不在连接查询中,则显示列为空

I have 2 tables "cars" and "insur_docs" and a query that joins 2 tables and send the query data to a function which is called in my view. But the problem is that I want to display all cars from table cars not only cars from join. So if a car has no id in table "insur_doc" to display empty column "status" and if the car has id at table "insur_doc" to display result that get from function that I have crated. controller

$result = DB::select('select c.*, i.sgs, i.tpl, i.kasko, i.inter_permis from cars as c inner join insur_docs as i where i.car_id = c.id');
    $date = Carbon::now();
    $limit_date = Carbon::now()->addMonths(1);
    return View::make('pages.index', array(

                'result' => $result,
                'date' => $date,
                'limit_date' => $limit_date
    ));
}

view

 @foreach ($result as $r)
          <tr>    
             <td>{{ $r->id }}</td>                                  
             <td>                                   
               {{ link_to_action('CarController@show',$r->Make, $r->id)}}</td>  
               {{ Form::open(array('action' => 'CarController@show', $r->id)) }}
               {{ Form::close() }}
             <td>{{ $r->License }}</td>  
             <td>{{ $r->Milage }}</td>                                                    
             <td>
@if( Helpers\Helper::sum($r->sgs, $r->tpl, $r->kasko, $r->inter_permis, $date, $limit_date) >= 0 && (Helpers\Helper::sum($r->sgs, $r->tpl, $r->kasko, $r->inter_permis, $date, $limit_date) <= 4))  
{{ HTML::image("assets/img/success_20.png", "Success") }}
 @elseif ( Helpers\Helper::sum($r->sgs, $r->tpl, $r->kasko, $r->inter_permis, $date, $limit_date) >= 5 && (Helpers\Helper::sum($r->sgs, $r->tpl, $r->kasko, $r->inter_permis, $date, $limit_date) <= 20)) 
{{ HTML::image("assets/img/warning_20.png", "Warning")  }}
 @else 
 {{ HTML::image("assets/img/danger_20_1.png", "Danger")  }}
@endif
</td>                                            
</tr>                                                            
@endforeach

I am displaying only values that get from join, but I also want to display other cars and status column to be empty.

  • 写回答

1条回答 默认 最新

  • duanduan1993 2014-05-12 15:05
    关注

    Use the query this way

    
        select c.*, i.sgs, i.tpl, i.kasko, i.inter_permis 
        from cars as c 
        left join insur_docs as i  on i.car_id = c.id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看