如何在laravel 5.2中为连接表做sql count函数

时间:2016-09-07 05:37:10

标签: php mysql laravel-5.2

我有两张桌子,'devicelist'和'devicegroup'。 devicelist表包含accountID,groupID,deviceID。 devicegroup表包含groupID,description。 在设备列表中,groupID表示组标识码,deviceID表示设备名称(它是唯一的),同一组ID将重复用于多个设备。 在设备组groupID中与设备组相同,groupID是唯一的,描述是组名。在我的视图页面中,需要显示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>

1 个答案:

答案 0 :(得分:0)

加入并且相同标准的位置没有做任何事情。使用此查询,您将获得设备,按groupID对它们进行分组并按groupID计数。

$vehicleCount=DB::table('devicelist as dev_list')
    ->select('dev_list.groupID, count(*)'))
    ->join('devicegroup as dev_group', 'dev_list.groupID', '=', 'dev_group.groupID')
    ->groupBy('dev_list.groupID');

要回答评论中的问题,最好这样做,虽然你去数据库两次,一次用于其他事情,一次用于groupID的计数,它更具可读性。