如何将MYSQL查询移动到Laravel Eloquent子查询?

时间:2015-11-20 17:21:04

标签: php mysql laravel laravel-5 eloquent

我的查询是这样的:

SELECT AVG(c.total) FROM (
    SELECT COUNT(*) as total, b.`app_id` FROM (
         SELECT DISTINCT `app_id`, `insurance_id`
         FROM `application`
         WHERE `price` IS NOT NULL AND `created_at` >= "2015-11-01 00:00:00"
         GROUP BY `app_id`, `insurance_id`
    ) as b
    GROUP BY b.`app_id`
) as c

如何将此查询实施到Laravel Eloquent?

我的表数据是这样的;

+------+--------+--------------+---------------+---------------------+
| id   | app_id | insurance_id | price         | created_at          |
+------+--------+--------------+---------------+---------------------+
| 3867 |  33846 |           10 | NULL          | 2015-11-20 17:50:26 |
| 3866 |  33846 |            4 | 642,42        | 2015-11-20 17:50:17 |
| 3865 |  33846 |            7 | 687.68        | 2015-11-20 17:50:02 |
| 3864 |  33846 |            8 | NULL          | 2015-11-20 17:49:56 |
| 3863 |  33846 |            3 | 540.7500      | 2015-11-20 17:49:55 |
| 3862 |  33846 |            1 | 988.240000000 | 2015-11-20 17:49:54 |
| 3861 |  33846 |            2 | NULL          | 2015-11-20 17:49:45 |
| 3860 |  33845 |           10 | 565.80        | 2015-11-20 17:22:25 |
| 3859 |  33845 |            4 | 504,90        | 2015-11-20 17:22:15 |
| 3858 |  33845 |            5 | 459.65        | 2015-11-20 17:22:04 |
| 3856 |  33845 |            7 | 664.58        | 2015-11-20 17:21:56 |
| 3857 |  33845 |           10 | 565.80        | 2015-11-20 17:21:56 |
| 3855 |  33845 |            8 | NULL          | 2015-11-20 17:21:54 |
| 3854 |  33845 |            1 | 987.420000000 | 2015-11-20 17:21:48 |
| 3853 |  33845 |            3 | 518.6900      | 2015-11-20 17:21:47 |
| 3852 |  33845 |            2 | NULL          | 2015-11-20 17:21:44 |
| 3851 |  33845 |            4 | NULL          | 2015-11-20 17:21:31 |
| 3850 |  33845 |            7 | NULL          | 2015-11-20 17:21:30 |
| 3849 |  33845 |            5 | 459.65        | 2015-11-20 17:21:26 |
| 3848 |  33845 |            3 | 518.6900      | 2015-11-20 17:21:23 |
+------+--------+--------------+---------------+---------------------+

1 个答案:

答案 0 :(得分:0)

我认为最简单的解决方案是使用DB接口。

您可以像这样运行查询:

$averages = \DB::select('SELECT AVG(c.total) FROM (
    SELECT COUNT(*) as total, b.`app_id` FROM (
           SELECT DISTINCT `app_id`, `insurance_id`
           FROM `application`
           WHERE `price` IS NOT NULL AND `created_at` >= "2015-11-01 00:00:00"
           GROUP BY `app_id`, `insurance_id`
        ) as b
        GROUP BY b.`app_id`
    ) as c');

您可以使用$averages来查看php artisan tinker的外观及其包含的内容以及可用的方法,这是一个非常有用的工具,可用于尝试找到问题的解决方案。< / p>

如果要动态设置created_at子句,可以执行以下操作:

$dateString = '2015-10-15 00:00:00';
$averages = \DB::select('SELECT AVG(c.total) FROM (
    SELECT COUNT(*) as total, b.`app_id` FROM (
           SELECT DISTINCT `app_id`, `insurance_id`
           FROM `application`
           WHERE `price` IS NOT NULL AND `created_at` >= "?"
           GROUP BY `app_id`, `insurance_id`
        ) as b
        GROUP BY b.`app_id`
    ) as c', [$dateString]);

如果您需要将此类查询的结果转换为Model,请查看Model#hyrdrate,您甚至可以跳过使用DB并直接转到{{3}的模型但是,因为此查询只返回一个可能不是您要查找的列。