From: Daevid Vincent
Date: April 18 2006 2:33am
Subject: How can I use a value computed in my SQL query for further computations?
List-Archive: http://lists.mysql.com/mysql/196964
MIME-Version: 1.0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
Here is a paired down version of a query I want to make. How can I get the
"grandtotal" column? I know about the "HAVING" clause, but that's only going
to be good for weeding out rows I don't want. I just want to do some basic
math here.
SELECT a.*,
DATE_FORMAT(a.created_on,'%m/%d/%y %h:%i:%s %p') AS
created_on_format,
DATE_FORMAT(a.timestamp,'%m/%d/%y %h:%i:%s %p') AS timestamp_format,
(views * ppview) AS totalviews,
(clicks * ppclick) AS totalclicks,
totalviews + totalclicks AS grandtotal
FROM advertisements a;
There has got to be a better way than this (which would be a colossal waste
of computing power to recalculate something that was just done!):
SELECT a.*,
DATE_FORMAT(a.created_on,'%m/%d/%y %h:%i:%s %p') AS
created_on_format,
DATE_FORMAT(a.timestamp,'%m/%d/%y %h:%i:%s %p') AS timestamp_format,
(views * ppview) AS totalviews,
(clicks * ppclick) AS totalclicks,
((views * ppview) + (clicks * ppclick)) AS grandtotal
FROM advertisements a;