M
M
MikMik2019-12-10 02:51:47
Yii
MikMik, 2019-12-10 02:51:47

How to rewrite query in ActiveRecord?

Please help me to rewrite query into ActiveRecord view

SELECT a.campaign_id, 
(SELECT COUNT(a1.`action`) FROM `action` a1 WHERE a1.campaign_id = a.campaign_id AND a1.`action`=10) pass,
(SELECT COUNT(a2.`action`) FROM `action` a2 WHERE a2.campaign_id = a.campaign_id AND a2.`action`=20) unique_pass, 
(SELECT COUNT(a3.`action`) FROM `action` a3 WHERE a3.campaign_id = a.campaign_id AND a3.`action`=30) purchases 
FROM `action` a 
LEFT JOIN `campaign` c ON a.`campaign_id` = c.`id`
WHERE c.`user_id`=:user_id GROUP BY a.campaign_id

I don’t understand how to set a dynamic condition in a subquery a1.campaign_id = a.campaign_id AND a1.`action`=10

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question