簡體   English   中英

如何在Laravel 5.2中為聯接表執行SQL計數功能

[英]How to do sql count function in laravel 5.2 for join table

我有兩個表,“設備列表”和“設備組”。 devicelist表包含accountID,groupID,deviceID。 devicegroup表包含groupID,描述。 在devicelist中,groupID表示組標識代碼,而deviceID是設備名稱(唯一),多個設備將重復相同的groupID。 與設備列表相同的設備組groupID中,組ID是唯一的,描述是組名。 在我的視圖頁面中,需要顯示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>

加入並在相同條件下不執行任何操作。 通過此查詢,您將獲得設備,按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的次數,則可讀性更高。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM