内连接只返回7条记录
我有一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将其更改为:
我怀疑状态和帐户链接之间的所有记录都不匹配。执行左连接将选择每个
status
,无论accountLink
是否匹配。Try changing it to this:
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 notaccountLink
has a match.除非列数据类型不同,否则 JOIN 语法应该有效。
根据IN()的MySQL文档:
确保列类型匹配应确保 JOIN 语法正常工作。
The JOIN syntax should work, unless the column data types are different.
Per the MySQL Documentation for IN():
Ensuring that your column types match should ensure that the JOIN syntax works correctly.
您确实想要使用内部联接,因为您只想在“status”表有一条记录并且在“accountLink”表中找到相应的用户记录时返回结果。如果“状态”表记录没有相应的用户条目,则不应显示它(至少根据您的帖子)。即使 accountLink 表中没有匹配的条目,LEFT OUTER JOIN 也会显示状态表记录。
这是学习 SQL 连接的绝佳资源:
SQL 联接 (w3schools.com)
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)