patternsqlModerate
Is possible to reuse a already calculatesd column into a query in MySQL?
Viewed 0 times
columnintoquerypossiblealreadymysqlreusecalculatesd
Problem
I have a query with complicated column, and I would like to use this column result in another column, for instance:
but his query returns an error:
How can I reuse a already calculated field?
SELECT ( /* Complex query */ ) as myValue, if( myValue > 10, "OK" , "" ) from table;but his query returns an error:
Error Code: 1054. Unknown column 'myValue' in 'field listHow can I reuse a already calculated field?
Solution
You can use user-defined variable.
Maybe server demands preliminary variable definition. If so,
The documentation do not specified the order of output values calculations, but in practice it always matched the order they are written.
SELECT @temp := ( /* Complex query */ ) AS myValue,
IF ( @temp > 10, "OK" , "" )
FROM table;Maybe server demands preliminary variable definition. If so,
SELECT @temp := ( /* Complex query */ ) AS myValue,
IF ( @temp > 10, "OK" , "" )
FROM table, (SELECT @temp := 0) dummy;The documentation do not specified the order of output values calculations, but in practice it always matched the order they are written.
Code Snippets
SELECT @temp := ( /* Complex query */ ) AS myValue,
IF ( @temp > 10, "OK" , "" )
FROM table;SELECT @temp := ( /* Complex query */ ) AS myValue,
IF ( @temp > 10, "OK" , "" )
FROM table, (SELECT @temp := 0) dummy;Context
StackExchange Database Administrators Q#214986, answer score: 14
Revisions (0)
No revisions yet.