mysql简单子查询问题

发布于 2024-11-14 03:03:57 字数 708 浏览 2 评论 0原文

table user
____________________________________________
id    name    nickname    info_id
1     john    apple        11
2     paul    banana       12
3     pauline melon        13

table info
_____________________________________________
id    job         location 
11    model       usa
12    engineer    russia
13    seller      brazil

result I want
______________________________________________
1   john    apple    model     usa

我的查询

left join:

select * from user a left join info b on b.id = a.info_id where a.id=1

subquery:

select a.*, b.* from (user a, info b) where b.id = a.info_id

哪个更好?

table user
____________________________________________
id    name    nickname    info_id
1     john    apple        11
2     paul    banana       12
3     pauline melon        13

table info
_____________________________________________
id    job         location 
11    model       usa
12    engineer    russia
13    seller      brazil

result I want
______________________________________________
1   john    apple    model     usa

my query

left join:

select * from user a left join info b on b.id = a.info_id where a.id=1

subquery:

select a.*, b.* from (user a, info b) where b.id = a.info_id

which is better?

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

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

发布评论

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

评论(1

凡间太子 2024-11-21 03:03:57
SELECT a.`name`, a.`nickname`, b.`job`, b.`location` 
FROM `user` AS a
LEFT JOIN `info` AS b 
    ON ( a.`info_id` = b.`id` )

这应该是相当有效的。如果您担心的话,请尝试使用 MySQL EXPLAIN(还要确保 ID 字段上有索引):
http://dev.mysql.com/doc/refman/5.1 /en/using-explain.html


更新

在看到您还没有遇到性能问题后,我不会担心它。 “不要修复未损坏的东西”。如果您发现它将来速度变慢,或者该功能遇到瓶颈,那么请担心。

我给出的查询应该非常有效。

SELECT a.`name`, a.`nickname`, b.`job`, b.`location` 
FROM `user` AS a
LEFT JOIN `info` AS b 
    ON ( a.`info_id` = b.`id` )

That should be pretty efficient. Try using MySQL EXPLAIN if you are concerned (also make sure there are indexes on the ID fields):
http://dev.mysql.com/doc/refman/5.1/en/using-explain.html


UPDATE

After seeing that you are not having performance problems just yet, I would not worry about it. "Don't fix what ain't broken". If you find that it is slowing down in the future, or it is bottle-necking on that function, then worry about it.

The query I gave should be pretty efficient.

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