gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:20:28 27 4
gpt4 key购买 nike

我正在制作一个系统,我需要检查第一个表或第二个表中是否存在电子邮件。

    $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'");
}

我想制作一个mysql,这样就不需要两个了。由于两个表结构不同,UNION 并不是没有给出正确的结果。有没有一种没有UNION或JOINS

的方法

我一直在尝试使用 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

但问题是,如果表 2 的情况满足,则考虑表 1 的字段,并且所有值都会不匹配

最佳答案

我找到了自己问题的解决方案,所以只是想分享它

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

关于php - 如果第一个查询没有返回结果,则使用第二个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51274290/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com