Answer the question
In order to leave comments, you need to log in
How to write such a SQL query as an ActiveRecord?
I have this SQL query:
update users u_
join (
select m.user_id id, u.name name, SUM(IF(m.`status` = 'new' or m.`status` = 'newest', 1, 0)) new_marks_total
from marks m
left join users u on u.id = m.user_id
where m.`status` in ('oldest', 'old', 'new', 'newest') and m.user_id is not null and u.name is not null
group by m.user_id
) t1 on t1.id = u_.id
set u_.marks_total = t1.new_marks_total
where u_.id = <USER_ID>;
Answer the question
In order to leave comments, you need to log in
Why write this on AR?
Do you want to get the user model after the update?
1. Completely on ActiveRecord will not work, because it is only for ANSI SQL, and your update of two tables will only work in MySQL.
You can partially:
echo Mark::find()
->select(['user_id', 'name', 'new_marks_total' => new Expression('...')])
->with('user')
->where(['status' => ['oldest', 'old', 'new', 'newest']])
->andWhere(['IS NOT', 'user_id', null])
->andWhere(['IS NOT', 'name', null])
->groupBy('user_id')
->createCommand()
->rawSQL;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question