2019-05-09 15:26:47 121次浏览 1条回答 0 悬赏 50 金钱
SELECT sum(saleroom) as saleroom, `brand`, `goods_name`, sum(gross_margin) as gross_margin, sum(gross_margin)/(select sum(gross_margin) as saleroom_sum from `yn_goods_analyze_shop_by_day` where `create_date` BETWEEN '2019-04-29' AND '2019-05-05')*100 as gross_percent FROM `yn_goods_analyze_shop_by_day` WHERE `create_date` BETWEEN '2019-04-29' AND '2019-05-05' GROUP BY `spid`

请问大佬 这句 SQL 用 AR 查询怎么实现,日期部分用的是占位符

亿速云

最佳答案

  • 醉丶春风 发布于 2019-05-09 16:07 举报
    $subQuery = (new \yii\db\Query())->from('yn_goods_analyze_shop_by_day')
        ->select([
            'saleroom_sum' => 'sum(gross_margin)',
        ])
        ->where(['between', 'create_date', $startTime, $endTime])
        ->createCommand()
        ->getRawSql();
    
    $query = (new \yii\db\Query())->from('yn_goods_analyze_shop_by_day')
        ->select([
            'saleroom' => 'sum(saleroom)',
            'brand',
            'goods_name',
            'gross_margin' => 'sum(gross_margin)',
            'gross_percent' => "sum(gross_margin) / ($subQuery) * 100",
        ])
        ->where(['between', 'create_date', $startTime, $endTime])
        ->groupBy('spid');
    
    echo $query->createCommand()->getRawSql();
    
    1 条回复
    回复于 2019-05-09 16:19 回复

    感谢大佬

    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
EizoPeter
经理

EizoPeter

注册时间:2018-08-30
最后登录:5分钟前
在线时长:15小时39分
  • 粉丝4
  • 金钱1425
  • 威望0
  • 积分1575

热门问题