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
我的查询
左连接:
select * from user a left join info b on b.id = a.info_id where a.id=1
子查询:
select a.*, b.* from (user a, info b) where b.id = a.info_id
哪个更好?
答案 0 :(得分:2)
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
<强>更新强>
在看到您尚未遇到性能问题后,我不担心。 “不要修复没有破坏的东西”。如果你发现它在将来放慢速度,或者在这个功能上出现瓶颈,那么就要担心它。
我提出的查询应该非常有效。