gpt4 book ai didi

mysql - 从一个表中选择全部并在结果中填充另一表中的值

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

我想从一个表中获取完整列,并从另一个具有相同 id 的表中选择总和。

例如:

 table1          table2 
id target id target achived
1 40 1 20
2 50 2 25
3 66
4 80

我想从表 1 中选择所有内容并在其上填写实现的目标结果。

例如:

 id    target  target achived
1 40 20
2 50 25
3 66 0
4 80 0


how can i do that using mysql

最佳答案

使用此查询:

 select table1.*, case when table2.target_achieved is null 
then 0
else table2.target_achieved
end as target_achieved
from table1 left join table2
on table1.id = table2.id
order by table1.id

关于mysql - 从一个表中选择全部并在结果中填充另一表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44481706/

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