A
A
Abdula Magomedov2017-07-12 17:03:32
MySQL
Abdula Magomedov, 2017-07-12 17:03:32

Is it possible to somehow use the "alias" of a column in calculations of another column?

Hello! There is here such sql request.

SELECT
  `первый`,
  `что то длинное с вычислениями` AS "left_payd",
  `третий`                        AS "price",
FROM `откуда то`

In order not to do the calculations of the second column again, I want to do this. But it gives an error that the column was not found.
Can this be done in some other way? Maybe some temporary variables? I don't want to do the calculations again.
SELECT
  `первый`,
  `что то длинное с вычислениями` AS "left_payd",
  `третий`                        AS "price",
  `left_payd` / `price`           AS "left_payd_in_percent"
FROM `откуда то`

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2017-07-12
@Avarskiy

SELECT
   field1,
   field2,
   field1/field2 AS field3
FROM
   (
   SELECT
       ... AS field1,
       ... AS field2
   FROM ...
   ) t

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question