【问题标题】:Exclude join columns from Laravel scope query ONLY_FULL_GROUP_BY error从 Laravel 范围查询中排除连接列 ONLY_FULL_GROUP_BY 错误
【发布时间】:2019-07-18 12:40:18
【问题描述】:

我在 Laravel 项目中有一个范围查询,它隐式获取我不希望在结果集中出现的两列,因为它们会导致 ONLY_FULL_GROUP_BY 错误,我不想禁用此数据库条件。

我们有以下关系:

组织有 -> 类别

public function categories()
{
    return $this->belongsToMany(
        Category::class,
        'organisation_unit_template_categories',
        'organisation_unit_id',
        'template_category_id'
    );
}

类别有 -> 模板

public function templates()
{
    return $this->hasMany(Template::class);
}

模板有 -> 尺寸

public function dimensions()
{
    return $this->belongsTo(Dimensions::class, 'dimensions_id');
}

我们的类别也有一个范围查询,这样我们就可以得到所有包含至少一个维度为'digital = 0'的模板的类别

public function scopeIsPrint($query)
{
    return $query
        ->select($this->getTable().'.*')
        ->join('templates', 'template_categories.id', '=', 'templates.category_id')
        ->join('template_dimensions', 'template_dimensions.id', '=', 'templates.dimensions_id')
        ->where('template_dimensions.digital', 0)
        ->groupBy($this->getTable().'.id');
}

我们从控制器调用范围查询,如下所示:

$categories = $this->organisation->categories()->isPrint()->get();

这是输出:

SELECT 
    `template_categories`.*,
    `organisation_unit_template_categories`.`organisation_unit_id` AS `pivot_organisation_unit_id`,
    `organisation_unit_template_categories`.`template_category_id` AS `pivot_template_category_id`
FROM
    `template_categories`
        INNER JOIN
    `organisation_unit_template_categories` ON `template_categories`.`id` = `organisation_unit_template_categories`.`template_category_id`
        INNER JOIN
    `templates` ON `template_categories`.`id` = `templates`.`category_id`
        INNER JOIN
    `template_dimensions` ON `template_dimensions`.`id` = `templates`.`dimensions_id`
WHERE
    `organisation_unit_template_categories`.`organisation_unit_id` = 2
        AND `template_dimensions`.`digital` = 0
        AND `template_categories`.`deleted_at` IS NULL
GROUP BY `template_categories`.`id`

如何确保这两列:

`organisation_unit_template_categories`.`organisation_unit_id` AS `pivot_organisation_unit_id`,
`organisation_unit_template_categories`.`template_category_id` AS `pivot_template_category_id`

不包括在查询中,并且让我知道为什么它们首先被隐式添加。

非常感谢

【问题讨论】:

    标签: php mysql database laravel eloquent


    【解决方案1】:

    我们的类别也有一个范围查询,这样我们就可以得到所有包含至少一个维度为'digital = 0'的模板的类别

    我的建议是重写查询以使用exists 而不是joingroup by

    public function scopeIsPrint($query)
    {
        return $query
            ->whereExists(function($q) {
                return $q->selectRaw('1')->from('templates')
                         ->join('template_dimensions', 'template_dimensions.id', '=', 'templates.dimensions_id')
                         ->whereRaw('template_categories.id=templates.category_id')
                         ->where('template_dimensions.digital', 0)
            })
    }
    

    【讨论】:

    • 感谢您的推荐 - 这实际上比我最初所做的要好。 -- 使用专为解决问题而设计的 MySQL 函数,来解决它恰好可以解决的不同问题。
    猜你喜欢
    • 2015-10-06
    • 2018-06-18
    • 2014-02-17
    • 2012-01-15
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多