简体   繁体   English

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

[英]MySQL SELECT with left join returns null on values from joined table

Lets say I have two simple tables, programs and seasons :假设我有两个简单的表、 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 |

Nothing super fancy really, so now I run the following:真的没有什么超级花哨的,所以现在我运行以下命令:

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

Leaving me with an expected result of:给我留下了预期的结果:

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

However the actual result is as follows:但实际结果如下:

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

What is causing this and how to remedy?这是什么原因造成的,如何补救?

根据您给出的表结构,它应该是seasons.season 而不是seasons.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM