我有3个表,foo,foo2bar和bar。foo2bar是foo和bar之间的多对多映射。这里是内容。
select * from foo +------+ | fid | +------+ | 1 | | 2 | | 3 | | 4 | +------+ select * from foo2bar +------+------+ | fid | bid | +------+------+ | 1 | 1 | | 1 | 2 | | 2 | 1 | | 2 | 3 | | 4 | 4 | +------+------+ select * from bar +------+-------+------+ | bid | value | zid | +------+-------+------+ | 1 | 2 | 10 | | 2 | 4 | 20 | | 3 | 8 | 30 | | 4 | 42 | 30 | +------+-------+------+
我要请求的是:“给我所有zid为30的fid和值的列表”
我希望所有fid都能得到答案,因此结果如下所示:
+------+--------+ | fid | value | +------+--------+ | 1 | null | | 2 | 8 | | 3 | null | | 4 | 42 | +------+--------+
SELECT * FROM foo LEFT OUTER JOIN (foo2bar JOIN bar ON (foo2bar.bid = bar.bid AND zid = 30)) USING (fid);
在MySQL 5.0.51上测试。
这不是子查询,它仅使用括号指定连接的优先级。