帮助处理 LEFT JOIN SQL 查询中的 WHERE
我正在尝试构建一个查询,其中包含一个指示用户是否已下载文档的列。 我有一个名为 HasDownloaded 的表,其中包含以下列:id、documentID、memberID。 查明用户是否下载了特定文档很容易; 但我需要生成一个查询,其结果如下所示:
name id
----------------------
abc NULL
bbb 2
ccc 53
ddd NULL
eee 13
ID 并不重要; 我感兴趣的是文档是否已下载(是否为NULL)。
这是我的查询:
SELECT Documents.name, HasDownloaded.id FROM Documents
LEFT JOIN HasDownloaded ON HasDownloaded.documentID = Documents.id
WHERE HasDownloaded.memberID = @memberID
问题是,只有当 HasDownloaded 表中存在指定用户的条目时,这才会返回值。 我想保持简单,HasDownloaded 中仅包含已下载文档的条目。 因此,如果用户 1 已经下载了 abc、bbb 和 ccc,我仍然希望 ddd 和 eee 显示在结果表中,只是 id 为 NULL。 但 WHERE 子句只给出存在条目的值。
我不是一个 SQL 专家 - 有没有一个运算符可以给我我想要的东西? 我应该采取不同的方法吗? 或者这是不可能的?
I'm trying to construct a query that will include a column indicating whether or not a user has downloaded a document. I have a table called HasDownloaded with the following columns: id, documentID, memberID. Finding out whether a user has downloaded a specific document is easy; but I need to generate a query where the results will look like this:
name id
----------------------
abc NULL
bbb 2
ccc 53
ddd NULL
eee 13
The ID isn't really important; what I'm interested in is whether the document has been downloaded (is it NULL or not).
Here is my query:
SELECT Documents.name, HasDownloaded.id FROM Documents
LEFT JOIN HasDownloaded ON HasDownloaded.documentID = Documents.id
WHERE HasDownloaded.memberID = @memberID
The problem is, this will only return values if an entry exists for the specified user in the HasDownloaded table. I'd like to keep this simple and only have entries in HasDownloaded for documents that have been downloaded. So if user 1 has downloaded abc, bbb, and ccc, I still want ddd and eee to show up in the resulting table, just with the id as NULL. But the WHERE clause only gives me values for which entries exists.
I'm not much of a SQL expert - is there an operator that will give me what I want here? Should I be taking a different approach? Or is this impossible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 WHERE 子句中的条件移至连接条件。
每当您想要在 WHERE 子句中引用左连接表时,这是必需的。
Move the condition in the WHERE clause to the join condition.
This is necessary whenever you want to refer to a left join-ed table in what would otherwise be the WHERE clause.
将是这样做的正常方法。 有些人会将其缩短为:
正如 Matt B. 所示,您可以在 JOIN 条件下执行此操作 - 但我认为这更容易让人们感到困惑。 如果您不明白为什么将其移动到 JOIN 子句有效,那么我强烈建议远离它。
would be the normal way to do that. Some would shorten it to:
You can, as Matt B. shows, do it in your JOIN condition - but I think that's much more likely to confuse folks. If you don't understand WHY moving it to the JOIN clause works, then I'd strongly suggest staying away from it.
@Mark:我明白为什么 JOIN 语法有效,但感谢您的警告。 我确实认为你的建议更直观。 我很好奇哪个更有效率。 所以我进行了一个快速测试(恐怕这相当简单,只进行了 14 行和 10 次试验):
在 JOIN 条件下:
30.9 WHERE 子句:
看起来 WHERE 子句的效率稍微高一些。 有趣的! 再次感谢你们俩的帮助。
@Mark: I understand why the JOIN syntax works, but thanks for the warning. I do think your suggestion is more intuitive. I was curious to see which was more efficient. So I ran a quick test (this was rather simplistic, I'm afraid, over only 14 rows and 10 trials):
In the JOIN condition:
In the WHERE clause:
It looks like the WHERE clause is ever-so-slightly more efficient. Interesting! Once again, thanks to both of you for your help.