Answer the question
In order to leave comments, you need to log in
How to sort rows in mysql by average?
Hello! There is a sign like this:
[--order--]
1
2
3
4
5
6
Answer the question
In order to leave comments, you need to log in
There is no muscle at hand, the code is under Oracle, but the idea, I think, is clear:
with inp as (
select 1 id from dual union all
select 2 from dual union all
select 3 from dual union all
select 4 from dual union all
select 5 from dual union all
select 6 from dual)
SELECT
id
FROM
inp
ORDER BY
abs( (
SELECT
AVG(id)
FROM
inp
) - id),
id;
select
table.col,
subquery.avg,
ABS(table.col - subquery.avg) as diff
from table,
(select AVG(table.col) as avg from table) as subquery
order by
diff
Add the necessary data to form the sort field (average value) as a left join, and calculate the sort field.
Keep in mind that this will be slow.
select *, (abs(avg_column-column)) as sorting_column from orders i
left join (select avg(column) as avg_column from orders) o on...
order by sorting_column
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question