我有两张表如下:
Suppliers:
supplier_id, supplier_pw
123456 zzzzz
647890 yyyyy
Customers:
customer_id, customer_pw
00000 zzzzz
11111 yyyyy
我已将两个表的ID设置为唯一键和主键。 运行以下查询后:
$ username = 123456,$ password = zzzzzz。
SELECT * FROM suppliers, customers WHERE (suppliers.supplier_id = '$username' AND suppliers.supplier_pw = '$password') OR (customers.customer_id = '$username' AND customers.customer_pw = '$password'
它返回来自供应商的结果,其中id = 123456也是来自客户的密码= zzzzzz的结果。 但我想让它只返回匹配id和密码的那个。 我应该在哪里更改查询?
更新表格结构:
供应商:
supplier_id(PK,unique),supplier_pw,supplier_lastname,supplier_firstname,supplier_email(unique),supplier_phone,company_name(unique),company_address,company_city,company_state,company_zip
客户:
customer_id(PK,unique),customer_pw,customer_lastname,customer_firstname,customer_email(unique),customer_phone,customer_address,customer_city,cusomter_state,customer_zip
答案 0 :(得分:0)
如果要选择与用户名和密码输入匹配的数据,如果匹配,则需要来自两个表,然后您可以使用union all
作为
select
supplier_id as id,
supplier_lastname as lastname,
supplier_firstname as firstname,
supplier_email as email ,
supplier_phone as phone,
'Supplier' as entity_name
from suppliers
where suppliers.supplier_id = '$username' AND suppliers.supplier_pw = '$password'
union all
select
customer_id as id,
customer_lastname as lastname,
customer_firstname as firstname,
customer_email as email,
customer_phone as phone,
'Customer' as entity_name
customers.customer_id = '$username' AND customers.customer_pw = '$password'
请注意,我从两个表中选择了几个列,并添加了可以添加更多内容的别名,但是请确保两个查询具有相同的选择数,然后添加一个名为entity_name
的额外列来告诉您如果你有来自两个表的匹配数据,那么该行的表格就会出现。