referencing mySQL query components

199 views Asked by At

Is there a way to make reference to parts of a MySQL query within the same query?

For example:

SELECT 50000 AS starting_principle, .065*50000 AS interest, 
    principle + interest AS principle_plus_interest

The third column, principle_plus_interest in the query set gives me an error. Is there a way to code this besides writing 50000 + .065*50000 AS principle_plus_interest?

1

There are 1 answers

2
Mureinik On BEST ANSWER

You cannot refer to aliases in the select list (or the where clause, for that matter). One way around this is to use a subquery:

SELECT starting_principle, 
       interest, 
       principle + interest AS principle_plus_interest 
FROM   (SELECT 50000 AS starting_principle, .065*50000 AS interest
        FROM   some_table) t