same query multiple tables mysql
I have 2 tables in mysql db
T1 : registration
T2 : multiple_email
and I have a field email_id in both the tables. I want to run a query to see if a email is valid. So, I can run 2 queries as
select count(*) from registr开发者_StackOverflowation where emailid = $emailid
select count(*) from multiple_email where emailid = $emailid
and if one of the count is greater than 0, I consider it as a valid email.
Is there an elegant way of doing it in single query?
select count(*) +
(select count(*) from multiple_email where emailid = $emailid)
from registration where emailid = $emailid
I think it's better to use 2 queries with some changes:
select emailid from registration where emailid = $emailid ;
select emailid from multiple_email where emailid = $emailid
It will be faster than count(*) because you will read from index only. If emailid is not primary key you can add limit 1.
精彩评论