Laravel计数行值> 0

时间:2015-03-18 12:19:10

标签: mysql laravel

我一直在尝试为我的查询找到解决方案。但我还没有能够解决它。大多数都很好用,但计数部分不像我想要的那样工作。

我的查询如下:

        $years = Sample::whereCostumerId($id)
        ->groupBy('year')
        ->orderBy('year', 'DESC')
        ->get(array(
            DB::raw('year'),
            DB::raw('COUNT(id) as antalProver'),
            DB::raw('MIN(provnr) AS minProvnr'),
            DB::raw('MAX(provnr) AS maxProvnr'),
            DB::raw('count(P_HCl) AS numP_HCl'),
            DB::raw('count(Total_lerhalt) AS numLerhalt'),
            DB::raw('ROUND(AVG(pH),1) AS avgpH'),
            DB::raw('ROUND(AVG(P_AL),1) AS avgP_AL'),
            DB::raw('ROUND(AVG(K_AL),1) AS avgK_AL'),
            DB::raw('AVG(X) AS coordExist')
        ));

这里的问题是P_HCl和Total_lerhalt列中的许多行都包含零。而且我不想数这些。我只想计算值大于零的地方。 我觉得那里有一些很好的解决方案。 如果你们有任何其他的查询解决方案,所以我们很高兴看到它。

由于

1 个答案:

答案 0 :(得分:0)

你大部分都在这里 - 你只需要在查询中添加where子句:

$years = Sample::whereCostumerId($id)
    ->groupBy('year')
    ->orderBy('year', 'DESC')
    ->where('year', '>', 0)
    ->get(array(
        DB::raw('year'),
        DB::raw('COUNT(id) as antalProver'),
        DB::raw('MIN(provnr) AS minProvnr'),
        DB::raw('MAX(provnr) AS maxProvnr'),
        DB::raw('count(P_HCl) AS numP_HCl'),
        DB::raw('count(Total_lerhalt) AS numLerhalt'),
        DB::raw('ROUND(AVG(pH),1) AS avgpH'),
        DB::raw('ROUND(AVG(P_AL),1) AS avgP_AL'),
        DB::raw('ROUND(AVG(K_AL),1) AS avgK_AL'),
        DB::raw('AVG(X) AS coordExist')
    ));