开发者

mysql simple subquery problem

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.i开发者_如何学JAVAd=1

subquery:

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

which is better?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜