Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

creating a new temp column in mysql with some math

1:04 am on Nov 10, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 21, 2002
posts: 1560
votes: 0

Ok, I have some numeric columns, and I want to establish and average for them.
col 1 value = 4
col 2 value = 7
col 3 value = 3
col 4 value = 9
I want to be able to create (4+7+3+9)/4, as another column in a query. Then I want to use this number as for ORDER BY for another table.
With the numbers table there is another column that has an id relating to this other table.
I really can't get my head around this. It's hard enough to explain. Let me know if you need more help in explaining.
9:49 am on Nov 10, 2012 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
votes: 0

alter table X add column average_float float(6,2) <- or simlar
update X set average_float = (col1+col2+col3+col4)/4;
in your Y table which references X:
select * from Y (.....) order by (select average_float from X where X.id = Y.id) desc

or similar...