laravel过滤产品按季节价格折扣

o3imoua4  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(398)

laravel通过折扣获得季节价格,并通过价格asc过滤产品。
我有这张table

+----+------------+------------+------------+-------+------+----------+------------+------------+------------+
| id | base_price | startDate  | endDate    | cost  | type | Duration | product_id | created_at | updated_at |
+----+------------+------------+------------+-------+------+----------+------------+------------+------------+
|  1 |     100.00 | 1537390800 | 1538773200 | 95.00 | day  |        2 |          9 | NULL       | NULL       |
|  2 |     100.00 | 1537390800 | 1538773200 | 85.00 | day  |        3 |          9 | NULL       | NULL       |
|  3 |     100.00 | 1537390800 | 1538773200 | 75.00 | day  |        4 |          9 | NULL       | NULL       |
|  4 |     100.00 | 1537390800 | 1538773200 | 70.00 | day  |        5 |          9 | NULL       | NULL       |
+----+------------+------------+------------+-------+------+----------+------------+------------+------------+

和产品表

+----+----------------------------+-----------+-----+-------+---------+--------------+---------------------+---------------------+---------------------+
| id | alias                      | status    | img | price | user_id | published_at | deleted_at          | created_at
 | updated_at          |
+----+----------------------------+-----------+-----+-------+---------+--------------+---------------------+---------------------+---------------------+
|  8 | toyota-corolla-1-6-elegant | draft     | 18  | 30    |       1 | 2018-08-14   | NULL                | 2018-08-14 15:06:12 | 2018-08-20 14:58:18 |
|  9 | test                       | published |     | 0     |       1 | 2018-08-23   | 2018-09-10 19:44:29 | 2018-08-23 14:45:18 | 2018-09-10 19:44:29 |
+----+----------------------------+-----------+-----+-------+---------+--------------+---------------------+---------------------+---------------------+

如你所见,我们有三种可能的选择
1) 如果我们没有在选定日期的季节表价格,我们使用产品表中的价格
2) 如果我们有基本价格,但没有,但没有折扣,我们使用基本价格
3) 如果我们选择2天(并进入日期范围),则在本例中使用字段成本95美元
我试过这段代码,但它只显示一个产品,而此时我需要以动态价格显示所有产品。

$products_obj->join('ec_season', function ($join) use($days) {
            $join->on( 'ec_products.id', '=', 'ec_season.product_id' )
                 ->where( 'ec_season.cost', DB::raw( "(
    select min(ec_season.cost)
    from ec_season
    where  `ec_season`.`product_id` = `ec_products`.`id`  and `ec_season`.`type` = 'day' and `Duration` <= '3'
)
" ) );
        });

我用的是拉威尔5.6。你能帮我做这个吗?
我可以通过mysql用final cost(final\u cost)创建新字段吗?它可以按最终成本排序
编辑1
新代码,但它仍然返回重复的产品,并显示最终的成本“=>”ec\ U季节。成本“而不是价格

$products_obj->leftJoin('ec_season', 'ec_season.product_id', '=', 'ec_products.id')         
                     ->select('*',
                DB::raw("COALESCE('ec_season.cost', 'ec_season.base_price', 'products.price') as final_cost"))
                                     ->where('ec_season.type', 'day')
                                     ->where('ec_season.Duration', '<=', 3)
                                     ->orderBy('final_cost', 'desc');

编辑2或者更容易选择所有产品,并为每个产品计算正确的价格,然后按价格对集合进行排序,并执行另一个sql请求,该请求按指定id的顺序发出。或者,它将比使用一个sql查询执行的所有操作都慢?例如我们有100种产品

uklbhaso

uklbhaso1#

像这样的东西可能更好,但我不确定我是否捕获了你的所有需求。也未经测试。

// use a left join to select all products, not just those with a related entry in the seasonal table
$products_obj->leftJoin('ec_season', 'ec_season.product_id', '=', 'ec_products.od')
    // maybe need to update the * to only select columns you need and prevent collisions
   // but just showing my idea which is to use COALESCE
    ->select('*', DB::raw("COALESCE('ec_season.cost', 'ec_season.base_price', 'products.price') as final_cost")
    ->where('ec_season.type', 'day')
    ->where('ec_season.Duration', '<=', 3)
    ->orderBy('final_cost', 'desc');

合并mysql函数

相关问题