/    Sign up×
Community /Pin to ProfileBookmark

PHP query to divide one column by another

Hello
I have recently learned how to place rows in descending order of a certain column, and taking the top values.

Right now I want to place rows in a descending order of [B]ONE COLUMN DIVIDED BY ANOTHER COLUMN[/B]

I have looked all over the internet and cant find anything. Please help.

To make it clear, if this is my table:

id | value | number
1 |__20__|_5
2 |__30__|_
6
3 |__5___|_5

I would like to rearrange this in descending order of value divided by number.
So that order would be 2, 1, 3

because 30/6 = 5, 20/5 = 4, 5/5 = 1,
5 4 1

Thanks.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@scragarApr 21.2007 — how are you ordering them to begin with?
Copy linkTweet thisAlerts:
@spliffauthorApr 21.2007 — i found the solution for myself. it was very obvious ?

[code=php]
$sql = 'SELECT * FROM tablename ORDER BY column1/column2 DESC LIMIT 1';

$query = mysql_query($sql);

$data = mysql_fetch_assoc($query);

echo $data['id']; [/code]


hope this helps people with the same question
×

Success!

Help @spliff spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...