MYSQL INNER JOIN其中一行的属性不存在

时间:2014-08-31 21:52:46

标签: mysql sql left-join inner-join right-join

我有一个Mysql表(CONFIGS),如下所示:

+---------+
| config  |
+---------+
| ie8     |
| Firefox |
| Chrome  |
+---------+

我有另一张表(PROJECTS),看起来像:

+---------+----------------+--------------+
| config  | name           | passed_count |
+---------+----------------+--------------+
| Firefox | Project 1      |            0 |
| Chrome  | Project 1      |            3 |
| Chrome  | Project 2      |            1 |
| Firefox | Project 2      |            0 |
| ie8     | Project 2      |            0 |
+---------+----------------+--------------+

我希望最终结果如下:

+---------+----------------+--------------+
| config  | name           | passed_count |
+---------+----------------+--------------+
| Firefox | Project 1      |            0 |
| Chrome  | Project 1      |            3 |
| ie8     | Project 1      |            0 |
| Chrome  | Project 2      |            1 |
| Firefox | Project 2      |            0 |
| ie8     | Project 2      |            0 |
+---------+----------------+--------------+

基本上我希望所有PROJECTS与所有CONFIGS

相关联
| ie8     | Project 1      |            0 |

这是我想要添加的其他字段

我尝试了各种LEFT JOINS,RIGHT JOINS但是无法让它工作。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

您需要生成所有可能的组合,然后使用left join。这是一个例子:

select c.config, n.name, coalesce(p.passed_count, 0) as passed_count
from config c cross join
     (select distinct name from projects p) n left join
     projects p
     on p.name = n.name and p.config = c.config;