带有左联接的MySQL SELECT对联接表中的值返回null

时间:2019-12-11 15:56:10

标签: mysql

假设我有两个简单的表,programsseasons

programs
| id | season_id | title     |
| 1  | 1         | Program 1 |
| 2  | 1         | Program 2 |
| 3  | 2         | Program 3 |

seasons
| id | season   |
| 1  | Season 1 |
| 2  | Season 2 |

真的没有超级幻想,所以现在运行以下命令:

SELECT
    programs.title, seasons.season 
FROM 
    programs 
LEFT JOIN seasons ON programs.season_id = seasons.id;

给我一​​个预期的结果:

| title     | season   |
| Program 1 | Season 1 |
| Program 2 | Season 1 |
| Program 3 | Season 2 |

但是实际结果如下:

| title     | season   |
| Program 1 | NULL     |
| Program 2 | NULL     |
| Program 3 | NULL     |

这是什么原因以及如何补救?

1 个答案:

答案 0 :(得分:0)

根据您提供的表格结构,应该是seasons.season而不是seasons.name