MySql re-using subquery result

0

In mysql is there a way where I can re-use the result of a subquery inside the main query?

select
(select resultado from tabela limit 1) as resultado_subquery,
(resultado_subquery * outra_coluna) as teste
from tabela
    
asked by anonymous 13.11.2018 / 12:11

1 answer

2

You can do something like the example below:

select
subquery.resultado,
(subquery.resultado * outra_coluna) as teste
from tabela
left join (select codigo, resultado from tabela limit 1) as subquery on (tabela.codigo = subquery.codigo)

In short, you can by the subquery in LEFT JOIN as a temporary table, but it will have to relate to some main SQL key, for example the CODE column is the key.

    
13.11.2018 / 12:26