【问题标题】:How to do sql count function in laravel 5.2 for join table如何在 laravel 5.2 中为连接表执行 sql 计数功能
【发布时间】: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


    【解决方案1】:

    加入并且在相同的条件下没有做任何事情。通过此查询,您将获得设备,按 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 的计数,但它更具可读性。

    【讨论】:

    • 使用 sql 我只想从 devicelist 表中获取每个 groupID 中的设备计数。我已经通过了剩余的 groupID 和描述。我在我的问题中添加了完整的代码。所以请告诉是否可以这样发送,或者我应该使用相同的 sql 查询发送所有数据。感谢您的宝贵回复:)
    • 我已根据您的最新评论更新了答案。抱歉,花了我这么多时间。
    猜你喜欢
    • 1970-01-01
    • 2016-08-30
    • 2016-03-08
    • 2017-03-18
    • 2023-03-31
    • 2016-09-15
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多