内连接只返回7条记录

发布于 2024-10-01 04:53:19 字数 522 浏览 1 评论 0原文

我有一个包含 500.000 条记录的表,其中充满了 Twitter 更新。然后我有一张包含用户信息的表。

我基本上需要我的用户表中人员的所有 Twitter 记录。

我可以用这个 SELECT IN 查询来做到这一点:

SELECT *
FROM STATUS WHERE twitterUserID
IN (    
    SELECT twitteruserid
    FROM accountLink
)

但这显然非常慢。

然后我尝试使用连接来完成此操作,但它只显示 7 条记录。不知道为什么。

SELECT status . * , accountLink.userId, accountLink.twitterUserId
FROM status
JOIN accountLink
ON status.twitterUserId = accountLink.twitterUserId

有谁知道什么可能导致这种行为以及如何解决它?

I've got a table with 500.000 records filled with Twitter updates. Then I've got a table with user info.

I basically need all the Twitter records of the people in my user table.

I can do it with this SELECT IN query:

SELECT *
FROM STATUS WHERE twitterUserID
IN (    
    SELECT twitteruserid
    FROM accountLink
)

But that's obviously very slow.

I then tried to do it with a join, but it only shows 7 records. No idea why.

SELECT status . * , accountLink.userId, accountLink.twitterUserId
FROM status
JOIN accountLink
ON status.twitterUserId = accountLink.twitterUserId

Does anyone know what could cause this behaviour and how to solve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

栀子花开つ 2024-10-08 04:53:19

尝试将其更改为:

SELECT status.* , accountLink.userId, accountLink.twitterUserId
FROM status
LEFT JOIN accountLink
ON status.twitterUserId = accountLink.twitterUserId

我怀疑状态和帐户链接之间的所有记录都不匹配。执行左连接将选择每个 status,无论 accountLink 是否匹配。

Try changing it to this:

SELECT status.* , accountLink.userId, accountLink.twitterUserId
FROM status
LEFT JOIN accountLink
ON status.twitterUserId = accountLink.twitterUserId

I suspect that there aren't matches for all the records between status and account link. Doing a left join will select every status regardless of whether or not accountLink has a match.

征棹 2024-10-08 04:53:19

除非列数据类型不同,否则 JOIN 语法应该有效。

根据IN()的MySQL文档

然后使用二分搜索来完成对该项目的搜索。这意味着如果 IN 值列表完全由常量组成,IN 会非常快。否则,类型转换将根据第 11.2 节“表达式求值中的类型转换”中描述的规则进行,但适用于所有参数。

确保列类型匹配应确保 JOIN 语法正常工作。

The JOIN syntax should work, unless the column data types are different.

Per the MySQL Documentation for IN():

The search for the item then is done using a binary search. This means IN is very quick if the IN value list consists entirely of constants. Otherwise, type conversion takes place according to the rules described in Section 11.2, “Type Conversion in Expression Evaluation”, but applied to all the arguments.

Ensuring that your column types match should ensure that the JOIN syntax works correctly.

一刻暧昧 2024-10-08 04:53:19
SELECT s.*, a.twitterUserId, a.userId
FROM status AS s INNER JOIN accountLink AS a
WHERE s.twitterUserId=a.twitterUserId

您确实想要使用内部联接,因为您只想在“status”表有一条记录并且在“accountLink”表中找到相应的用户记录时返回结果。如果“状态”表记录没有相应的用户条目,则不应显示它(至少根据您的帖子)。即使 accountLink 表中没有匹配的条目,LEFT OUTER JOIN 也会显示状态表记录。

这是学习 SQL 连接的绝佳资源:
SQL 联接 (w3schools.com)

SELECT s.*, a.twitterUserId, a.userId
FROM status AS s INNER JOIN accountLink AS a
WHERE s.twitterUserId=a.twitterUserId

You DO want to use inner join because you only want to return results IF the "status" table has a record AND a corresponding user record is found in the "accountLink" table. If a "status" table record does NOT have a corresponding user entry, you shouldn't display it (at least according to your post). LEFT OUTER JOIN would display status table records even if there was not a matching entry in the accountLink table.

Here's a great resource for learning about SQL joins:
SQL Joins (w3schools.com)

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