我希望在使用Yii2
语法选择查询
select distinct pfs_id, unitprice, sum(qty)
from order_items
where order_id= id
and item_farm_id = id
group By pfs_id
我试了好几次但仍然没有运气,
请任何人帮助。
答案 0 :(得分:2)
以下查询返回您希望结果为数组。
$data=ModelName::find() //replace your model name here
->select('distinct(pfs_id),unitprice,sum(qty)')
->where(['order_id'=>id,'item_farm_id'=>id])
->groupBy('pfs_id')
->asArray->all();
您也可以使用别名:
$data=ModelName::find()
->select('distinct(pfs_id) as pfs_id,unitprice,sum(qty) as sum_of_qty')
->where(['order_id'=>id,'item_farm_id'=>id])
->groupBy('pfs_id')
->asArray->all();