Php 选择哪里或者从两张桌子上取
我需要一些帮助我有两张桌子:Php 选择哪里或者从两张桌子上取,php,mysql,Php,Mysql,我需要一些帮助我有两张桌子: table "clients" +------+-------------+-----------+ id | email | otherinfo| +------+-------------+-----------+ 1 |test@ts.ts | ..... | +------+-------------+-----------+ 2 |test2@.ts.ts | .... | +------+-
table "clients"
+------+-------------+-----------+
id | email | otherinfo|
+------+-------------+-----------+
1 |test@ts.ts | ..... |
+------+-------------+-----------+
2 |test2@.ts.ts | .... |
+------+-------------+-----------+
table "comptes"
+------+-------------+---------------+
id | login | id_clients |
+------+-------------+---------------+
1 | test | 1 |
+------+-------------+---------------+
1 | test2 | 2 |
+------+-------------+---------------+
etc. | etc. | etc.. |
+------+-------------+---------------+
在我的网站上,当用户创建一个帐户时,他会给出两个表的信息。所以我想在添加之前测试数据库中是否存在登录名或电子邮件,比如
'select clients.email,comptes.login
from clients,comptes
where clients.email='test2@.ts.ts'
or comptes.login ='test';
但是这个查询返回的结果是空的,我尝试了其他组合,但没有一个组合给出正确的结果。因此,我在这里搞砸了什么您需要使用连接来告诉mysql数据是如何关联的: 例如:
您需要使用连接来告诉mysql数据是如何关联的: 例如:
您需要明确标识您的联接字段。逗号分隔的连接语法在使用IMO时非常糟糕,可能会产生意想不到的结果。在您的情况下,它会尝试连接两个id列上的两个表。所以试试这个
SELECT clients.email, comptes.login
FROM clients INNER JOIN comptes on clients.id = comptes.id_clients
WHERE clients.email='test2@.ts.ts' OR comptes.login = 'test';
请注意,在这种情况下,您将返回两行,因为WHERE子句最终将为您提供客户端id 1和2。您需要明确标识您的联接字段。逗号分隔的连接语法在使用IMO时非常糟糕,可能会产生意想不到的结果。在您的情况下,它会尝试连接两个id列上的两个表。所以试试这个
SELECT clients.email, comptes.login
FROM clients INNER JOIN comptes on clients.id = comptes.id_clients
WHERE clients.email='test2@.ts.ts' OR comptes.login = 'test';
SELECT cl.email, co.login
FROM clients AS cl
INNER JOIN comptes AS co ON cl.id = co.id_clients
WHERE cl.email = 'test2@.ts.ts' OR co.login = 'test'
请注意,在这种情况下,您将返回两行,因为WHERE子句最终将为您提供客户端id 1和2。您根本不需要连接来查看它们是否存在。以下查询返回任何匹配记录的id:
SELECT cl.email, co.login
FROM clients AS cl
INNER JOIN comptes AS co ON cl.id = co.id_clients
WHERE cl.email = 'test2@.ts.ts' OR co.login = 'test'
select c.id, 'email' as matchtype
from clients c
where c.email = <email>
union all
select c.id, 'login' as matchtype
from comptes c
where c.login = <login>
这将为您提供匹配的ID,并告诉您如果感兴趣,副本将出现在何处。如果只需要0或1标志来指定是否存在重复项,请执行以下操作:
select count(*) as numdups
from ((select c.id, 'email' as matchtype
from clients c
where c.email = <email>
)
union all
(select c.id, 'login' as matchtype
from comptes c
where c.login = <login>
)
) t
您根本不需要加入来查看它们是否存在。以下查询返回任何匹配记录的id:
select c.id, 'email' as matchtype
from clients c
where c.email = <email>
union all
select c.id, 'login' as matchtype
from comptes c
where c.login = <login>
这将为您提供匹配的ID,并告诉您如果感兴趣,副本将出现在何处。如果只需要0或1标志来指定是否存在重复项,请执行以下操作:
select count(*) as numdups
from ((select c.id, 'email' as matchtype
from clients c
where c.email = <email>
)
union all
(select c.id, 'login' as matchtype
from comptes c
where c.login = <login>
)
) t
谢谢,这工作得很好,但是第二个查询返回这个错误“每个派生表都必须有自己的别名”@user1559104。我不知道为什么会这样。你有期末考试吗?我检查了sqlfiddle,在联合之前甚至不允许使用别名。我确实删除了最后的t,并且我使用mysql和phpmyadmin。无论如何,我使用了第一个解决方案thanksthanks,它工作得非常好,但是第二个查询返回这个错误“每个派生表都必须有自己的别名”@user1559104。我不知道为什么会这样。你有期末考试吗?我检查了sqlfiddle,在联合之前甚至不允许使用别名。我确实删除了最后的t,并且我使用mysql和phpmyadmin。不管怎样,我使用了第一种解决方案,谢谢