开发者

same query multiple tables mysql

开发者 https://www.devze.com 2023-03-30 15:08 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号