id为关联外键 所有的查询以辅表数据是否存在为准
主表A
id | name | type |
---|---|---|
1 | foo | 1 |
2 | bar | 2 |
3 | test | 1 |
4 | tes1 | 1 |
5 | tes2 | 1 |
6 | tes3 | 1 |
7 | tes4 | 1 |
8 | tes5 | 1 |
辅表B
id | color |
---|---|
4 | 1 |
5 | 2 |
6 | 3 |
辅表c
id | brand |
---|---|
1 | 44 |
2 | 21 |
3 | 12 |
想得到结果如表D
id 7 8 两条数据在辅表中不存在 也就不再显示
id | name | type | color | brand |
---|---|---|---|---|
1 | foo | 1 | null | 44 |
2 | bar | 2 | null | 21 |
3 | test | 1 | null | 12 |
4 | tes1 | 1 | 1 | null |
5 | tes2 | 1 | 2 | null |
6 | tes3 | 1 | 3 | null |
a.sql:
运行结果: