Answer the question
In order to leave comments, you need to log in
How to implement such a query in Yii2?
Hello! There is such a, relatively, not tricky request:
SELECT A.agency_id, A.agency_name, SUM( B.amount ) AS summ
FROM agency AS A
LEFT OUTER JOIN billing B ON B.date > '2006-05-01' AND B.date < '2006-07-20' AND B.agency_id = A.agency_id
GROUP BY A.agency_name
query = (new Query())
->select(['a.agency_name', 'b.amount'])
->from(['agency a'])
->leftJoin('billing b', "b.agency_id = a.agency_id AND b.date > '2006-05-01' AND b.date < '2007-07-20' ")
->groupBy('a.agency_name');
Answer the question
In order to leave comments, you need to log in
So far I've decided this:
$query = Yii::$app->db->createCommand("
SELECT A.agency_id, A.agency_name, SUM( B.amount ) AS summ
FROM agency AS A
LEFT OUTER JOIN billing B ON B.date > '2006-05-01'
AND B.date < '2006-07-20'
AND B.agency_id = A.agency_id
GROUP BY A.agency_name");
$result= $query->queryAll();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
For a start GROUP BY A.agency_name
, replace withGROUP BY A.agency_id, A.agency_name
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question