Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用groupby和count的Laravel完全联接_Php_Mysql_Laravel_Relational Database - Fatal编程技术网

Php 使用groupby和count的Laravel完全联接

Php 使用groupby和count的Laravel完全联接,php,mysql,laravel,relational-database,Php,Mysql,Laravel,Relational Database,我正在尝试用count(child)和all master raw(群比)在拉雷维尔获得大师级儿童。下面是表格记录和laravel代码 父表 +-------------+------------+ | Id | Name | +-------------+------------+ | 1 | a | +-------------+------------+ |

我正在尝试用count(child)和all master raw(群比)在拉雷维尔获得大师级儿童。下面是表格记录和laravel代码

父表

    +-------------+------------+  
    | Id          | Name       |
    +-------------+------------+  
    | 1           | a          |
    +-------------+------------+    
    | 2           | b          |
    +-------------+------------+ 
    | 3           | c          |
    +-------------+------------+
子表

    +-------------+------------+  
    | parent_Id   | Code       |
    +-------------+------------+
    | 1           | d1         |
    +-------------+------------+ 
    | 1           | d1         |
    +-------------+------------+    
    | 1           | d1         |
    +-------------+------------+ 
    | 1           | d1         |
    +-------------+------------+    
    | 2           | d2         |
    +-------------+------------+ 
    | 2           | d3         |
    +-------------+------------+ 
结果 如果子级的主id不大于0,则统计子级中所有相同的父id并显示所有主id

    +-------------+----------------------+ 
    |countparentIds in child| parentName       
    +-------------+----------------------+ 
    | 6                     | a          |
    +-------------+----------------------+  
    | 2                     | b          |
    +-------------+----------------------+  
    | 0                     | c          |
    +-------------+----------------------+
现在我的拉威尔雄辩如下图所示

 $master = DB::table('parents')
        ->select(array('parent.name as Name', DB::raw('COUNT(child.parent_id) as countparentids')))
        ->join('child', 'parent.id', '=', 'child.parent_id', 'left outer')
        ->where(child.code,'d1')
        ->orderBy('parent.name')
        ->groupBy('parent.id')
        ->groupBy('parent.name')
        ->get();

现在,我得到了结果,父表所有行,并计算具有父id的子项。

请在
像这样

   $ter = DB::table('parents')
    ->leftjoin('child', 'parent.id', '=', 'child.parent_id')
    ->select(array('parent.name as Name', DB::raw('CASE WHEN (child.code= "d1") THEN COUNT(child.parent_id) ELSE 0 END as countparentids')))
    ->orderBy('parent.name')
    ->groupBy('parent.name')
    ->get();


像这样

   $ter = DB::table('parents')
    ->leftjoin('child', 'parent.id', '=', 'child.parent_id')
    ->select(array('parent.name as Name', DB::raw('CASE WHEN (child.code= "d1") THEN COUNT(child.parent_id) ELSE 0 END as countparentids')))
    ->orderBy('parent.name')
    ->groupBy('parent.name')
    ->get();

您的问题是什么?结果不会显示所有父行,它会显示子行计数,但不会显示所有父行,我要查找所有父行名称countofchild_parentid。因为instanc子表的代码为d1,所以结果应该是child=4中的第一行CountParentId,parentname=a countparentid在child=0中,parentname=b countparentid在child=0中,parentname=cuse leftJoin而不是join有什么问题?结果不会显示所有父行,它确实显示子计数,但不是所有父行,我正在查找所有父名称countofchild\u parentid。因为instanc子表的代码为d1,所以结果应该是child=4中的第一行CountParentId,child=0中的parentname=a CountParentId,parentname=b countparentid在child=0,parentname=cuse leftJoin而不是join中我的代码是leftJoin join('table','parent.column','=','childcol','left outer')。好的,这是正确的,您需要在case-bases上给出where条件。您所说的case-bases是什么意思。当您编写
->where(child.code,'d1')
时,您没有得到所需的o/p,只需尝试一下,检查您是否得到了o/p。我的代码是leftjoin join('table','parent.column','=','childcol','leftouter')).好的,这是正确的,您需要在案例基础上给出where条件您所说的案例基础是什么意思。当您编写
->where(child.code,'d1')
时,您没有得到所需的o/p,请尝试一下,检查您是否得到了o/p