【问题标题】:How to get the default price of the product with different prices from different date periods from the period closest to today?如何从最接近今天的时期获取不同日期时期不同价格的产品的默认价格?
【发布时间】:2021-06-08 02:02:51
【问题描述】:

my sqlfiddle eample

你好,

根据上面的sqlfiddle例子;

我有一张表 A,其中列出了产品,还有一张表 B,其中列出了与这些产品相关的不同时期的不同价格。

这里我根据用户选择的日期显示这些价格。没有问题。

但是,如果用户没有选择日期,默认情况下我无法显示最接近今天的时段的价格。

在我给出的例子中,sql查询成功了,但是我不能以laravel查询的形式写成功。或者作为 Eloquent orm 查询

我该怎么做?

$query->select(['tableA.*', 'tableB.start_date', 'tableB.end_date', 'tableB.price'])
                            ->join('tableB', function($join) {
                            $join->on('tableA.id', '=', 'tableB.pro_id');
                            })->where(function($sq) use ($postFrom) {
                                $sq->when($postFrom[0]=='0', function ($syq) {
                                    $syq->whereRaw('DAYOFYEAR(curdate()) <= DAYOFYEAR(tableB.end_date)');
                                }, function ($stq) use ($postFrom) {
                                    $stq->whereDate('tableB.start_date', '<=', $postFrom[0])
                                       ->whereDate('tableB.end_date', '>=', $postFrom[0]);
                                });
                            })->orWhere(function($ssq) use ($postTo) {
                                $ssq->whereDate('tableB.start_date', '<=', $postTo[0])
                                    ->whereDate('tableB.end_date', '>=', $postTo[0]);
                    })->groupBy('tableA.id')->orderBy('tableB.price', $sortDirection);

note1: $postFrom 和 $postTo 是用户的开始和结束日期。如果用户没有提交日期,$postFrom 显示为 0。

注意2:我在满足 $postFrom[0] == '0' 条件时显示默认价格。

note3:例如,使用 sqlfiddle 示例中的 '2021-03-07' 值代替动态现值。

note4:根据本次查询,默认取第一期的价格值。但这不是我想要的。

注意5:我不能使用“joinSub”,因为 Laravel 版本是 5.5。

note6:在我要转换为Laravel Query形式的例子中,正常工作的sql查询没有任何问题:

select `tableA`.*, `tableB`.`start_date`, `tableB`.`end_date`, `tableB`.`price`  
from `tableA` 
right join( 
  SELECT id, start_date, end_date, pro_id, price, DATEDIFF(`tableB`.`end_date`, '2021-03-07') diff 
  FROM `tableB` GROUP BY id order by diff asc 
) `tableB` on `tableA`.`id` = `tableB`.`pro_id` where (date(`end_date`) >= '2021-03-07') 
  group by `tableA`.`id` order by `price` desc

【问题讨论】:

    标签: laravel eloquent octobercms eloquent-relationship octobercms-backend


    【解决方案1】:

    这是您查询的等效查询。我还没有执行。

    如果 Laravel 版本大于 5.5

    $query1 = DB::table('tableB')
              ->selectRaw("id, start_date, end_date, pro_id, price, DATEDIFF(end_date, '2021-03-07') AS diff")
             ->groupBy('id')->orderBy('diff','ASC');
        
    TableA::select('tableA.*', 'tableB.start_date', 'tableB.end_date', 'tableB.price')
             ->joinSub($query1, 'tableB', function ($join)
            {
                  $join->on('tableA.id', '=', 'tableB.pro_id');
            })
            ->whereDate('tableB.end_date','>=','2021-03-07')
            ->groupBy('tableA.id')->orderBy('price','DESC')->get();
    

    对于 Laravel 5.5

    TableA::select('tableA.*', 'tableB.start_date', 'tableB.end_date', 'tableB.price')
             ->join(DB::raw("(SELECT id, start_date, end_date, pro_id, price, 
             DATEDIFF(`tableB`.`end_date`, '2021-03-07') diff 
             FROM `tableB` GROUP BY id order by diff asc) table2 "), function ($join)
            {
                  $join->on('tableA.id', '=', 'table2.pro_id');
            })
            ->whereDate('table2.end_date','>=','2021-03-07')
            ->groupBy('tableA.id')->orderBy('price','DESC')->get();
    

    【讨论】:

    • 感谢您的回答。但我很抱歉没有提到 laravel 版本。我现在将其添加为注释。我不能使用“joinSub”,因为 Laravel 版本是 5.5。
    • 在这种情况下使用DB::raw
    • 我用不同的变体进行了测试,是的,它有效!太棒了,这几天我已经尝试了很多这样的事情。 :) 现在只剩下一个问题了。我们如何在 DB::raw 中动态发送“2021-03-07”部分?今天应该发送你的日期
    • '2021-03-07' 是当前日期还是可以是任何日期?
    • 你可以把它传入一个变量中。 $date = '2021-0307'
    猜你喜欢
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    相关资源
    最近更新 更多