我有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 | +------+-------+------+
我想要的是,"给我一个包含所有fid和值的列表,其中zid为30"
我期待所有fids的答案,所以结果如下:
+------+--------+ | fid | value | +------+--------+ | 1 | null | | 2 | 8 | | 3 | null | | 4 | 42 | +------+--------+
Bill Karwin.. 17
SELECT * FROM foo LEFT OUTER JOIN (foo2bar JOIN bar ON (foo2bar.bid = bar.bid AND zid = 30)) USING (fid);
在MySQL 5.0.51上测试过.
这不是子查询,它只是使用括号来指定连接的优先级.
SELECT * FROM foo LEFT OUTER JOIN (foo2bar JOIN bar ON (foo2bar.bid = bar.bid AND zid = 30)) USING (fid);
在MySQL 5.0.51上测试过.
这不是子查询,它只是使用括号来指定连接的优先级.
SELECT * FROM foo LEFT JOIN ( Foo2bar JOIN bar ON foo2bar.bid = bar.bid AND zid = 30 ) ON foo.fid = foo2bar.fid;
未经测试