Answer the question
In order to leave comments, you need to log in
How to properly query an Active Record?
write a request
$years=Serial::find()->from(['new_table'=>Serial::find()->orderBy(['year'=>SORT_DESC])])->select('year')->groupBy('year')->orderBy(['year'=>SORT_DESC])->all();
SELECT `year`
FROM (
SELECT *
FROM `fl_serial`
ORDER BY `year` DESC) `new_table`
GROUP BY `year`
SELECT `year`
FROM (
SELECT *
FROM `fl_serial`
) `new_table`
GROUP BY `year`
ORDER BY `year` DESC
Answer the question
In order to leave comments, you need to log in
Do you need to get a unique list of years? if so, then your initial query was not formed correctly, for these purposes you should not use grouping and a nested subquery, it is much easier to use DISTINCT, like this:
accordingly, the request for AR will be as follows
Serial::find()->select('year')->orderBy(['year'=>SORT_DESC])->distinct();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question