简体   繁体   English

两个表上的 SQL 连接,这两个表都为 WHERE 子句提供参数

[英]SQL Join on two tables that both give argument for WHERE clause

Does the following SQL Query work?以下 SQL 查询有效吗?

SELECT domains.domainURL, passwords.* FROM domains LEFT JOIN passwords ON domains.id = passwords.domainId WHERE domains.domainURL = {1} AND passwords.userId = {2};

The SQL Query shall receive the password that the user has saved for a specific domain(PW Manager). SQL 查询应接收用户为特定域(PW 管理器)保存的密码。 Therefore I have the user Id and the domainURL.因此我有用户 ID 和 domainURL。 I need to receive the domain Id from another table with the domainURL.我需要从另一个表中使用 domainURL 接收域 ID。 Then I need to receive the password from the passwords table with the user Id and the domainId as argument.然后我需要从密码表中接收密码,并将用户 ID 和 domainId 作为参数。

Will this query "work"?这个查询会“工作”吗?

SELECT d.domainURL, p.*
FROM domains d LEFT JOIN
     passwords p
     ON d.id = p.domainId
WHERE d.domainURL = {1} AND p.userId = {2};

Well, it should not result in an error -- assuming that the tables and columns are correct.好吧,它不应该导致错误——假设表和列是正确的。 Does it do what you want?它做你想要的吗? Well, if you want an INNER JOIN , it does what you want.好吧,如果你想要一个INNER JOIN ,它会做你想做的。

I suspect that you might intend:我怀疑你可能打算:

SELECT d.domainURL, p.*
FROM domains d LEFT JOIN
     passwords p
     ON d.id = p.domainId AND p.userId = {2} 
WHERE d.domainURL = {1};

This will return the row from passwords if it exists .如果存在,这将返回来自passwords的行。 It will return all rows from domains that match the WHERE clause.它将返回匹配WHERE子句的domains所有行。

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

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