Edit:

虽然这个问题最初是针对我下面描述的查询的,但我得到的答案几乎适用于所有与在Laravel中使用派生表/子查询相关的问题

Original Question:

最近我对laravel查询生成器有点着迷.它有一些非常好的特性,但我觉得它不是为更复杂的数据库操作而构建的.

这就是我试图构建的查询:

select 

'IFNULL(counted.product_count, 0) AS product_count', 
'uncounted.value', 
'uncounted.attribute_id', 
'uncounted.attribute_option_id' 

    from ( 

        select
        'counted.id', 
        'counted.attribute_id', 
        'counted.value', 
        'count(counted.attribute_id) AS product_count'

        from `attribute_options` as `counted` 
        where `counted.product_id` in (?, ?, ?, ?, ?) 
        group by `counted.attribute_option_id` 

    ) as 'counted' 

right join 'attribute_options' as 'uncounted'
        on 'counted.id' = 'uncounted.id' 

  group by 'attribute_option_id'

Explanation of the query:

我的try :

    $productIds = [ 1, 2, 3, 4, 5 ];

    $subQuery = \DB::table('attribute_options')->selectRaw('counted.id, counted.attribute_id, counted.value, count(counted.attribute_id) AS product_count')
                    ->from('attribute_options AS counted')
                    ->whereIn('counted.product_id', $productIds)
                    ->groupBy('counted.attribute_option_id')
                    ->mergeBindings($subQuery);

    $query = Model::selectRaw('IFNULL(counted.product_count, 0) AS product_count, uncounted.value, uncounted.attribute_id, uncounted.attribute_option_id')
                    ->from(\DB::raw(' ( ' . $subQuery->toSql() . ' ) AS counted '))
                    ->rightJoin('attribute_options AS uncounted', 'counted.id', '=', 'uncounted.id')
                    ->groupBy('attribute_option_id')
                    ->get();

请帮助我,因为我不喜欢使用DB::raw()或DB::select()语句.这不会让人觉得"拉拉维英语"或"Eloquent ".

推荐答案

你的第一次try 看起来很接近.试试这个:

I removed the long namespace reference and suggest you add a 100 statement to make your code more readable

$productIds = [ 1, 2, 3, 4, 5 ];

$subQuery = DB::table('attribute_options AS counted')->selectRaw('counted.id, counted.attribute_id, counted.value, count(counted.attribute_id) AS product_count')
                ->whereIn('counted.product_id', $productIds)
                ->groupBy('counted.attribute_option_id')

$query = AttributeOption::selectRaw('IFNULL(counted.product_count, 0) AS product_count, uncounted.value, uncounted.attribute_id, uncounted.attribute_option_id')
                ->from(\DB::raw(' ( ' . $subQuery->toSql() . ' ) AS counted '))
                ->mergeBindings($subQuery->getQuery())
                ->rightJoin('attribute_options AS uncounted', 'counted.id', '=', 'uncounted.id')
                ->groupBy('attribute_option_id')
                ->get();

Laravel相关问答推荐

为什么Laravel在API请求时返回BadMethodCallException?

Laravel:在 PHPUnit 的覆盖率报告中包含文件夹(如果在 app/ 文件夹之外)?

如何从laravel中的另一个表中获取用户名

在 laravel 中查询此类数据的最佳做法是什么?

验证错误后的 Laravel 自定义重定向

为什么命令php artisan serve不起作用

集成测试模拟外观与注入模拟

Laravel Passport Route [login] 未定义

Homebrew PHP 似乎没有链接

使用 laravel eloquent 关系检索除 NULL 之外的所有记录

在 laravel 分页中添加一些数据

如何使用外部图像生成 html div 的 screenshot截图?

Filesystem.php 中的 ErrorException

如何在 Laravel 5 中的每个相关输入字段旁边显示验证错误?

在 laravel 的测试套件中只运行一个单元测试

Laravel 5 - 从控制器级别的所有请求对象中删除参数

Laravel 4 - 一个表单中有两个提交按钮,并且两个提交都由不同的操作处理

如何判断是否连接到 Laravel 4 中的数据库?

Distinct values with pluck

Laravel - Union + 分页?