帮助处理 LEFT JOIN SQL 查询中的 WHERE

发布于 2024-07-08 03:16:16 字数 830 浏览 10 评论 0原文

我正在尝试构建一个查询,其中包含一个指示用户是否已下载文档的列。 我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晒暮凉 2024-07-15 03:16:16

将 WHERE 子句中的条件移至连接条件。

SELECT Documents.name, HasDownloaded.id FROM Documents
LEFT JOIN HasDownloaded ON HasDownloaded.documentID = Documents.id 
  AND HasDownloaded.memberID = @memberID 

每当您想要在 WHERE 子句中引用左连接表时,这是必需的。

Move the condition in the WHERE clause to the join condition.

SELECT Documents.name, HasDownloaded.id FROM Documents
LEFT JOIN HasDownloaded ON HasDownloaded.documentID = Documents.id 
  AND HasDownloaded.memberID = @memberID 

This is necessary whenever you want to refer to a left join-ed table in what would otherwise be the WHERE clause.

妄想挽回 2024-07-15 03:16:16
WHERE HasDownloaded.memberId IS NULL OR HasDownloaded.memberId = @memberId

将是这样做的正常方法。 有些人会将其缩短为:

WHERE COALESCE(HasDownloaded.memberId, @memberId) = @memberId

正如 Matt B. 所示,您可以在 JOIN 条件下执行此操作 - 但我认为这更容易让人们感到困惑。 如果您不明白为什么将其移动到 JOIN 子句有效,那么我强烈建议远离它。

WHERE HasDownloaded.memberId IS NULL OR HasDownloaded.memberId = @memberId

would be the normal way to do that. Some would shorten it to:

WHERE COALESCE(HasDownloaded.memberId, @memberId) = @memberId

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.

冷情妓 2024-07-15 03:16:16

@Mark:我明白为什么 JOIN 语法有效,但感谢您的警告。 我确实认为你的建议更直观。 我很好奇哪个更有效率。 所以我进行了一个快速测试(恐怕这相当简单,只进行了 14 行和 10 次试验):

在 JOIN 条件下:

AND HasDownloaded.memberID = @memberID
  • 客户端处理时间:4.6
  • 总执行时间:35.5
  • 服务器回复等待时间:

30.9 WHERE 子句:

WHERE HasDownloaded.memberId IS NULL OR HasDownloaded.memberId = @memberId
  • 客户端处理时间:7.7
  • 总执行时间:27.7
  • 服务器回复等待时间:22.0

看起来 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:

AND HasDownloaded.memberID = @memberID
  • Client processing time: 4.6
  • Total execution time: 35.5
  • Wait time on server replies: 30.9

In the WHERE clause:

WHERE HasDownloaded.memberId IS NULL OR HasDownloaded.memberId = @memberId
  • Client processing time: 7.7
  • Total execution time: 27.7
  • Wait time on server replies: 22.0

It looks like the WHERE clause is ever-so-slightly more efficient. Interesting! Once again, thanks to both of you for your help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文