gpt4 book ai didi

mysql - SQL 如果为 NULL 则插入

转载 作者:可可西里 更新时间:2023-11-01 08:07:45 25 4
gpt4 key购买 nike

是否有纯 SQL 方法来执行此操作?

我有一张表apples:id, price,还有一张apple_info:apple_id, color

对于 apples 中的每一行,我想在 apple_info 中添加相应的行(如果它尚不存在)。找不到这方面的任何例子。

最佳答案

试试这个:

insert into apple_info (apple_id, color)
select a.id, 'some_color'
from apples a
left outer join apple_info ai
on (a.id = ai.apple_id)
where ai.apple_id is null;

您将在 apple_info 上插入一行,其中 apple_idcolor 的固定值。我想这就是您想要的。

关于mysql - SQL 如果为 NULL 则插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7086164/

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