Laravel通过折扣获得季节价格,并按价格ASC过滤产品。
我有这个季节表
+----+------------+------------+------------+-------+------+----------+------------+------------+------------+
| 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)如果我们在base_price中有价格但没有价格但没有折扣 我们使用base_price
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'
)
" ) );
});
我使用laravel 5.6。 你能帮我吗?
我可以通过MySQL以最终成本(final_cost)创建新字段吗?并可以按final_cost
进行排序编辑1
新代码,但仍返回重复的产品并显示 final_cost“ =>” ec_season.cost“ 而不是价格
$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种产品
答案 0 :(得分:0)
像这样的事情可能会更好,但是不确定我是否满足了您的所有要求。也未经测试。
// 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');