Mysql Re-using subquery result

Asked

Viewed 238 times

0

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

select
(select resultado from tabela limit 1) as resultado_subquery,
(resultado_subquery * outra_coluna) as teste
from tabela

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 put the subquery in the LEFT JOIN as a temporary table, but you will have to relate with some key of the main SQL, as example the column CODE is the key.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.