gSide
gSide

Reputation: 39

PHP MySQL return divided result

I have query:

SELECT * FROM members ORDER BY ( firstColumn / anotherColumn ) DESC

Now I'm printing this:

return $query['firstColumn'] / $query['anotherColumn'];

Is it possible to print divided result without any php tricks? For example return $query['dividedColumns'];

Upvotes: 0

Views: 92

Answers (3)

Abeer Waseem
Abeer Waseem

Reputation: 72

You can try this

SELECT *, (firstColumn/anotherColumn) AS dividedColumns FROM members ORDER BY ( firstColumn / anotherColumn ) DESC

Upvotes: 0

Qirel
Qirel

Reputation: 26450

You can do it directly in the query, and use that result as the ORDER BY argument too.

SELECT *, firstColumn/anotherColumn as dividedColumns 
FROM members 
ORDER BY dividedColumns DESC

Now you can use it as $query['dividedColumns'] in PHP.

Upvotes: 1

Thai Duong Tran
Thai Duong Tran

Reputation: 2522

Column Alias will solve your problem, you can change your query to:

SELECT members.*, 
      ( firstColumn / anotherColumn ) AS `divided_amount`  
FROM members 
ORDER BY divived_amount DESC;

Upvotes: 2

Related Questions