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
哪个更好?
最佳答案
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
<小时/>更新
在看到您还没有出现性能问题后,我就不再担心了。 “不要修复未损坏的东西”。如果您发现将来速度变慢,或者该功能遇到瓶颈,那么请担心。
我给出的查询应该非常有效。
关于mysql简单子(monad)查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6247561/