1

I have stored function in MySql balance().

I want to select users with balance > 100;

This works

select user, balance() from users where balance() > 100

This don't

select user, balance as b from users where b > 100

I don't want to call balance() twice. How to write query as in 1 example?

1 Answer 1

2

MySQL extends the use of the having clause. When there is no explicit group by, you can use it like a where with table aliases:

select user, balance() as b
from users
having b > 100;

One alternative approach is to use a subquery. However, MySQL materializes subqueries, so that adds extra overhead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.