Laravel雄辩的计数具有多个数据范围

时间:2019-03-15 16:31:06

标签: laravel eloquent

我试图弄清楚如何创建Laravel查询,该查询将通过以下mysql查询生成。

select
 count(CASE WHEN points BETWEEN 0 AND 1.99 THEN 1 END) as count0_199,
 count(CASE WHEN points BETWEEN 2 AND 3.99 THEN 1 END) as count2_399,
 count(CASE WHEN points BETWEEN 4 AND 5.99 THEN 1 END) as count4_599
from
 stats

有可能以雄辩的方式做到吗?尚未在文档中看到它。 谢谢

1 个答案:

答案 0 :(得分:1)

据我所知,使用雄辩的方法执行此操作将涉及返回数据的某种格式:

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        byte[] input = "www.javaCODEgeeks.com".getBytes();
        byte[] keyBytes = new byte[]{0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd,


                (byte) 0xef};



        byte[] ivBytes = new byte[]{0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00};


        SecretKeySpec pKey = new SecretKeySpec(keyBytes, "DES");

        IvParameterSpec ivectorSpecv = new IvParameterSpec(ivBytes);

        Cipher c = Cipher.getInstance("DES/CBC/PKCS7Padding", "BC");


        System.out.println("input : " + new String(input));


        // encryption pass


        c.init(Cipher.ENCRYPT_MODE, pKey, ivectorSpecv);


        byte[] encr = new byte;


        int ctLen = c.update(input, 0, input.length, encr, 0);


        ctLen += c.doFinal(encr, ctLen);


        System.out.println("cipher: " + new String(encr).getBytes("UTF-8").toString() + " bytes: " + ctLen);
        c.init(Cipher.DECRYPT_MODE, pKey, ivectorSpecv);
        byte[] decrpt = new byte;
        int ptLen = c.update(encr, 0, ctLen, decrpt, 0);
        ptLen += c.doFinal(decrpt, ptLen);
        System.out.println("plain : " + new String(decrpt) + " bytes: " + ptLen);

在上面的代码中,将返回点在$results = []; $stats = Stat::where('points', '>=', '0') ->where('points', '<=', 5.99) ->get() ->each(function ($stat, $key) use (&$results) { if ($stat->points >= 0 && $stat->points <= 1.99) { $results['count0_199'][] = $stat; } if ($stat->points >= 2 && $stat->points <= 3.99) { $results['count2_399'][] = $stat; } if ($stat->points >= 4 && $stat->points <= 5.99) { $results['count4_599'][] = $stat; } }); return $results; 0之间的所有统计信息(因为它们无论如何都会返回)。然后,返回的集合将循环通过,以填充一个5.99数组,该数组将对返回的数据进行分组。

还有DB Facade

$results