gpt4 book ai didi

mysql - 如何在MySQL列中插入没有匹配的NULL值?

转载 作者:行者123 更新时间:2023-11-29 22:29:11 25 4
gpt4 key购买 nike

我有以下两个表:

项目消耗:

+---------------+------------+----------+
| project_name | project_id | Utilised |
+---------------+------------+----------+
| CStreamTax | 1 | 3500 |
| CStreamTravel | 2 | 149 |
+---------------+------------+----------+

项目:

+------------+---------------------+
| project_id | project_budgetstart |
+------------+---------------------+
| 1 | 15000 |
| 2 | 15000 |
| 3 | 500 |
+------------+---------------------+

我想制作一个两者的联合表,但使用 Utilized字段充满 NULL当项目表中有一个project_id时与projects_expished 表中的条目之一不匹配。

我的基本尝试如下:

SELECT projects.*, projects_expended.utilised
FROM projects_expended
JOIN projects
ON projects.project_id = projects_expended.project_id;

结果:

+------------+---------------------+----------+
| project_id | project_budgetstart | utilised |
+------------+---------------------+----------+
| 1 | 15000 | 3500 |
| 2 | 15000 | 149 |
+------------+---------------------+----------+

我明白为什么我会得到这样的结果。但我对此很坚持。我还进行了其他尝试(太多了,无法计数)。

最佳答案

我的答案来自:

LEFT JOIN vs. LEFT OUTER JOIN in SQL Server

这非常有帮助。

解决办法:

SELECT projects.*, projects_expended.utilised
FROM projects_expended
RIGHT JOIN projects
ON projects.project_id = projects_expended.project_id;

关于mysql - 如何在MySQL列中插入没有匹配的NULL值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29962122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com