0

我有一个如下所示的 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 |
+---------+----------------+--------------+

基本上我希望所有项目都与所有配置相关联

| ie8     | Project 1      |            0 |

这是我要添加的另一个字段

我尝试了各种 LEFT JOINS,RIGHT JOINS,但无法使其正常工作。任何人都可以帮助我吗?

4

1 回答 1

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;
于 2014-08-31T21:57:33.070 回答