MySQL 嵌入式 SELECT 与 JOIN

发布于 2024-08-18 16:02:26 字数 296 浏览 7 评论 0原文

之间有明显的区别吗?

SELECT userid, username, userdept,
    (SELECT deptname FROM depts WHERE deptid=userdept) AS deptname
    FROM users

SELECT userid, username FROM users
    INNER JOIN depts ON depts.deptid=users.userdept

哪个更好

Is there a noticeable difference between:

SELECT userid, username, userdept,
    (SELECT deptname FROM depts WHERE deptid=userdept) AS deptname
    FROM users

and

SELECT userid, username FROM users
    INNER JOIN depts ON depts.deptid=users.userdept

Which one is better?

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

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

发布评论

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

评论(4

半世晨晓 2024-08-25 16:02:27

您还可以看到关于此主题的 SO 中的许多讨论

you can see the many discussion in SO on this topic as well.

原谅过去的我 2024-08-25 16:02:27

这两个查询不是同义词。

如果您将 INNER JOIN 替换为 LEFT JOIN,它们将是同义词,但如果 deptid 则子查询可能会失败。不是唯一的,而 LEFT JOIN 总是会成功。

如果 depts.deptid 上有一个 UNIQUE 索引(这很可能是,因为该字段很可能是 PRIMARY KEY),那么性能差异可以忽略不计。

These two queries are not synonyms.

They would be the synonyms if you replaced the INNER JOIN with the LEFT JOIN, with the exception that the subquery is a subject to failure if deptid is not unique, while a LEFT JOIN will always succeed.

If there is a UNIQUE index on depts.deptid (which most probably is, since this field is most probably a PRIMARY KEY), then the performance difference would be negligible.

冷夜 2024-08-25 16:02:26

您的第二个查询具有更好的性能。

您可以看到这个示例: http://www.codersrevolution.com/index.cfm/2008/7/31/MySQL-performance-INNER-JOIN-vs-subselect

Your second query has better performance.

You can see this example: http://www.codersrevolution.com/index.cfm/2008/7/31/MySQL-performance-INNER-JOIN-vs-subselect

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