简体   繁体   English

如果第一个查询没有返回结果,则使用第二个查询

[英]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.

相关问题 查询未返回结果 - No results returned from query 在第二个查询和嵌套结果中使用第一个mysqli查询中的数据 - Using data from first mysqli query in second query and nesting results 尝试从第一个查询中获取结果以搜索第二个查询 - Trying to get the results from first query to search a second query 有没有一种方法可以查询一个MySQL数据库两次,并将第二个查询的结果添加到与第一个查询的结果相同的数组中? - Is there a way to query a MySQL DB twice and add the results from the second query to the same array as the results from the first query? 如何从第一个查询中获取字段的值并在第二个查询中使用它 - how to get a value of a field from first query and use it in second query 使用第一个查询的结果在查询中的第二个查询 - Second query within a query using results of first query 根据第一个查询的结果编写第二个查询-不起作用 - writing a second query based on results of the first - Not working 如果第一个查询未达到其限制,如何从第二个查询返回结果? - How do I return results from a second query if the first query didnt reach its limit? 使用查询结果插入第二张表-Codeigniter - Use query results to insert into second table - Codeigniter 如何检查结果是否从MySQL查询返回 - How to check if results are returned from a MySQL query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM