Yii 2.0 ActiveRecord 查询问题 [ 2.0 版本 ]
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 查询怎么实现,日期部分用的是占位符
最佳答案
-
$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 条回复
其他 0 个回答
没有找到数据。
EizoPeter
注册时间:2018-08-30
最后登录:2020-06-18
在线时长:34小时12分
最后登录:2020-06-18
在线时长:34小时12分
- 粉丝6
- 金钱2775
- 威望0
- 积分3115