Select two accounts in one request

I have a user (user, password, email), and I want to get the user counter, where user = 'someuser' and email count, where email = 'someemail' in one request, and I came up with the following:

SELECT (

SELECT COUNT( user )
FROM users
WHERE user = 'someuser'
), (

SELECT COUNT( email )
FROM users
WHERE email = 'someemail'
)
FROM users

But I wonder if there is a better way to do this? Thanks in advance:)

+5
source share
1 answer

No, this is the right way to do this in your case. Your calculations are likely to always be 0 or 1 and will be satisfied from the NC index.

If you want to scan more data, this may be more efficient for this:

select sum(case when user = 'x' then 1 end) UserCount, sum(case when email = 'x' then 1 end) EmailCount
from users

This will always check the table. It depends on data whose speed is higher. In your case, yours is faster.

+8
source

All Articles