[英]if no results returned from first query then use second query
I am making a system in which i need to check if a email exist in first table or second table. 我正在制作一个系统,需要在其中检查第一张表或第二张表中是否存在电子邮件。
$results = mysqli_query($db,"SELECT * FROM table_1 WHERE email='$email'");
if(count($results) == 0)
{
$results = mysqli_query($db,"SELECT * FROM table_2 WHERE email='$email'");
}
I wanted to make one mysql so that there is no need for two. 我想制作一个MySQL,这样就不需要两个了。 Since both table structures are different
UNION
is not not giving proper results. 由于两个表结构不同,因此
UNION
不能提供适当的结果。 Is there a way without UNION or JOINS
有没有
UNION or JOINS
的方法吗
I have been trying it with UNION 我一直在尝试与UNION
SELECT * FROM ( SELECT *, 1 as preference FROM table_1 WHERE email = 'doc@demo.com' UNION SELECT *, 2 as preference FROM table_2 WHERE email = 'doc@demo.com' ) T ORDER BY preference LIMIT 1
But the problem is if table 2 case satisfy then table 1 fields are considered and all values gets mismatched 但是问题是如果表2的大小写满足,则考虑表1的字段并且所有值都不匹配
I found a solution to my own question so just wanted to share it 我找到了自己问题的解决方案,所以只想分享一下
SELECT * FROM (
SELECT email,password,user_role, id as customer_id,id as user_id, 1 as preference
FROM table1 WHERE email = 'raj@demo.com'
UNION
SELECT email,password,user_role, customer_id,id, 2 as preference
FROM table2 WHERE email = 'doc@demo.com'
) T ORDER BY preference
LIMIT 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.