【发布时间】:2016-09-07 05:37:10
【问题描述】:
我有两个表,'devicelist' 和 'devicegroup'。 devicelist 表包含 accountID、groupID、deviceID。 devicegroup 表包含 groupID、描述。 在 devicelist 中,groupID 表示组标识码,deviceID 是设备名称(唯一),相同的 groupID 会重复多个设备。 devicegroup groupID 与 devicelist 相同,groupID 是唯一的,description 是组名。在我的视图页面中,需要显示 groupID、“devicegroup”表中的描述以及“devicelist”表中每个 groupID 中的设备数。每组设备的数量需要自动计算,而不是在where子句中给出一个groupID。
谁能告诉我如何编写 sql 查询,我需要做什么才能得到这个输出。 回复是可观的。 我尝试的查询如下。
public function getIndex()
{
$vehicleCount=DB::table('devicelist as dev_list')
->select(DB::raw('groupID, dev_group.description, count(*)'))
->join('devicegroup as dev_group', 'dev_list.groupID', '=', 'dev_group.groupID')->get();
echo $vehicleCount;
$groups = DB::table('devicegroup')->simplePaginate(10);
return view('group.groupAdmin')->with('groups',$groups);
}
更新的查询。
public function getIndex()
{
$vehicleCount=$vehicleCount=DB::table('devicelist as a')
->join('devicegroup as b', 'a.groupID', '=', 'b.groupID')
->select('b.groupID','b.description',DB::raw('count(a.deviceID)'))
->where('a.groupID','=','b.groupID')
->get();
//echo $vehicleCount;
//$groups = DB::table('devicegroup')->simplePaginate(10);
return view('group.groupAdmin')->with('vehicleCount',$vehicleCount);
}
我的查看页面是
@extends('app')
@section('content')
<br><br><br><br><br>
<div class="templatemo-content-wrapper">
<div class="templatemo-content">
<ol class="breadcrumb">
<li><a href="{{ url("/") }}"><font color="green">Home</font></a></li>
<li class="active">Group information</li>
</ol>
<h1>View/Edit Group information</h1>
<p></p>
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table id="example" class="table table-striped table-hover table-bordered">
<h3>Select a Group :</h3>
<thead>
<tr>
<th>Group ID</th>
<th>Group Name</th>
<th>Vehicle Count</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach($vehicleCount as $grp)
<tr>
<td>{{ $grp->groupID }}</td>
<td>{{ $grp->description }}</td>
<td>{{count($grp)}}</td>
<td>
<div class="btn-group">
<button type="button" class="btn btn-info">Action</button>
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
{{--@if ($nam->isActive == 'Yes')--}}
<li data-toggle="modal" data-target="#acceptModal" data-bookingid="{{ $grp->groupID }}"><a href="{{ url('/group/edit/'.$grp->groupID) }}">View/ Edit</a>
</li>
{{--@endif--}}
<li><a href="{{ url('/group/delete/'.$grp->groupID)}}">Delete</a></li>
</ul>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
{{--//{{$groups->links()}}--}}
</div>
</div>
</div>
</div>
</div>
</br>
【问题讨论】:
标签: php mysql laravel-5.2