- I am averaging 4 columns and giving an alias
score
. - Now, I am trying to use
score
alias inside GROUP_CONCAT to getrank
. - Everything works except when I add
(SELECT GROUP_CONCAT( score ORDER BY score DESC) FROM math )) AS rank
. - I understand it does not work because
score
does not exist in themath
table. But what do I need to do to make it work?
Here is my query in laravel 5.5 -
$ranking = DB::select('SELECT id, (a.addition_accuracy + a.subtraction_accuracy + a.multiplication_accuracy + a.division_accuracy)/4 as score, (SELECT GROUP_CONCAT( score ORDER BY score DESC) FROM math )) AS rank FROM math a where level = 5');
- I get 500 server error from above query.
- I am expecting below output
{
"id": 38,
"score": 99.24250030517578,
"rank": 1
},
{
"id": 51,
"score": 84.88500213623047,
"rank": 2
},
{
"id": 204,
"score": 69.27500057220459,
"rank": 3
}
]```