【问题标题】:Query count from selected data in join table连接表中选定数据的查询计数
【发布时间】:2019-03-10 19:24:36
【问题描述】:

我正在尝试获取 product_categories 和过滤器的所有组合以及为每个产品选择的过滤器计数。

我可以得到组合,但是我很难得到正确的计数。 我在 Laravel 工作并使用 \DB::raw 查询。

这是我目前得到的:

\DB::raw("SELECT product_categories.*,filters.name as filter, filter_types.heading as filter_type
                FROM product_categories, filters
                JOIN filter_types ON filter_types.id = filters.filter_type_id
                WHERE filter_types.type = 'Modules\\\DonaldRussell\\\ProductCategories\\\App\\\ProductCategory'
                ORDER BY product_categories.name, filter_type
                ");

db 架构如下:

Schema::create('filter_types', function(Blueprint $table) {
        $table->increments('id');
        $table->string('type')->notNull();
        $table->string('heading')->notNull();
        $table->timestamps();
    });

Schema::create('filters', function(Blueprint $table) {
        $table->increments('id');
        $table->integer('filter_type_id')->unsigned()->notNull();
        $table->foreign('filter_type_id')->references('id')->on('categories_types');
        $table->string('name')->notNull();
        $table->integer('order')->notNull();
        $table->integer('uses')->unsigned()->notNull()->default(0);
        $table->timestamps();
    });

Schema::create('product_categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name')->notNull();
        $table->string('slug')->notNull();
        $table->integer('order')->notnull()->unsigned()->default(0);
        $table->integer('type')->notnull()->unsigned()->default(1);
        $table->integer('seo')->nullable()->unsigned();
        $table->foreign('seo')->references('id')->on('seos')->onDelete('cascade');
        $table->timestamps();
    });

Schema::create('product_category_sets', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('category_id')->nullable()->unsigned();
        $table->foreign('category_id')->references('id')->on('product_categories')->onDelete('set null');
        $table->integer('sub_category_id')->nullable()->unsigned();
        $table->foreign('sub_category_id')->references('id')->on('tags')->onDelete('set null');
        $table->integer('sub_sub_category_id')->nullable()->unsigned();
        $table->foreign('sub_sub_category_id')->references('id')->on('tags')->onDelete('set null');
        $table->integer('product_id')->notnull()->unsigned();
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
        $table->timestamps();
    });

Schema::create('product_category_set_filters', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('product_category_set_id')->notnull()->unsigned();
        $table->foreign('product_category_set_id')->references('id')->on('product_category_sets')->onDelete('cascade');
        $table->integer('filter_id')->notnull()->unsigned();
        $table->foreign('filter_id')->references('id')->on('filters')->onDelete('cascade');
        $table->timestamps();
    });

在下面的屏幕截图中查看我需要的结果,但没有我无法工作的计数列:

【问题讨论】:

    标签: mysql laravel join


    【解决方案1】:

    首先,我不认为你的结构是正确的,但让我们假设它是正确的。

    我们需要 4 个模型: ProductCategorySet::class, Product::class, Category::class(product_category), 和Filter::class

    有了它们之间的适当关系,你就能做到

    ProductCategorySet::with('product')->with('category')->withCount('filters')->get();
    

    这就是你所需要的。

    use Illuminate\Database\Eloquent\Model;
    
    class ProductCategorySet extends Model
    {
        public function product()
        {
            return $this->belongsTo(Product::class);
        }
    
        public function category()
        {
            return $this->belongsTo(Category::class);
        }
    
        public function filters()
        {
            return $this->belongsToMany(Filter::class, 'product_category_set_filters', 'product_category_set_id', 'filter_id');
        }
    }
    

    【讨论】:

    • 我已经发布了我需要的结果图片。
    • @Marco 您尝试过我提出的解决方案吗?因为不推荐在 ORM 环境中使用DB::raw()。您的代码将无法维护。
    • 是的,但它不起作用。我在 ed 中通过联合查询得到了结果。
    • FROM product_categories, filters 使您的结果任意(错误)。
    猜你喜欢
    • 1970-01-01
    • 2021-10-07
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    相关资源
    最近更新 更多