简体   繁体   English

MySQL我可以从一个表还是另一个表中选择数据?

[英]MySQL Can I Select Data from One table OR another Table?

I'm working on a login script (In HTML, PHP and MYSQL) for a website that has two account types: Account "A" and Account "B". 我正在为具有两种帐户类型的网站开发登录脚本(HTML,PHP和MYSQL):帐户“ A”和帐户“ B”。 Right now I have a radio select that lets the user select which type of account they have. 现在,我有一个单选按钮,可让用户选择他们拥有的帐户类型。 But I want to clear up the login screen a bit and also enable the login form to fit in the header by removing the radio buttons completely. 但是我想稍微清除登录屏幕,并通过完全删除单选按钮使登录表单适合标题。

How do I get the script to check first one table for results (username and password) and if no results are found check the next table (username and password) and then once it finds a match sends it to an account specific page? 如何获取脚本以检查第一个表的结果(用户名和密码),如果找不到结果,请检查下一个表(用户名和密码),然后一旦找到匹配项,将其发送到特定于帐户的页面?

In a nutshell, I want to remove the need for the radio buttons. 简而言之,我想消除对单选按钮的需要。

Edit: I don't get any errors, but the problem is it only selects from the first table: Here's my code: 编辑:我没有收到任何错误,但问题是它仅从第一个表中选择:这是我的代码:

$q = "SELECT varone, vartwo 
    FROM tablea 
    WHERE (user_email='$e' AND pass=SHA1('$p'))
    UNION
    SELECT varthree, vartwo 
    FROM tableb
    WHERE (contact_email='$e' AND pass=SHA1('$p'))
    ";      
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
SELECT 'first' AS source, ...
FROM firstable
WHERE (username = $user) and (password = $password)

UNION

SELECT 'second' as source, ...
FROM secondtable
WHERE (....)

Then simply check where the results came from in your code. 然后只需检查结果在代码中的来源。

if ($row['source'] == 'first') { ... came from first table ... }

Should work, assuming the exact same username/password pair wouldn't appear in both tables. 应该可以正常工作,假设两个表中不会出现完全相同的用户名/密码对。

select * from table_A where id = 6 union select * from table_b where other_id = 6;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM