We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

sql to criteria

I Need help. There is a table of goods There is a price table The goods have the group_id attribute. Several goods with different prices can be combined into a group. Need to under GROUP BY group_id to do sorting on price. I managed to make such a request to the database

SELECT i.group_id
, min(istock.retail) price
FROM item i
LEFT JOIN item_stock istock ON istock.item_id = i.id

WHERE i.public = 1
AND i.`status` = 'actual'
AND i.id IN(10685, 2759, 10686, 1644, 894, 7302, 7328, 6717, 8885)
GROUP BY i.group_id
ORDER BY price ASC
;

But there is no field in the model

min(istock.retail) price

Can someone help shift this request to Phalcon\Mvc\Model\Criteria



252
Accepted
answer

I apologize for such a childish question. Got some sleep and realized. can be

min(istock.retail) 

to move in ORDER BY.

SELECT i.group_id
FROM item i
LEFT JOIN item_stock istock ON istock.item_id = i.id

WHERE i.public = 1
AND i.`status` = 'actual'
AND i.id IN(10685, 2759, 10686, 1644, 894, 7302, 7328, 6717, 8885)
GROUP BY i.group_id
ORDER BY min(istock.retail) ASC
;