gpt4 book ai didi

mysql - 更新查询在同一 id 未提及的行中插入 null

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

这是我的更新查询

UPDATE customer_entity_varchar
SET value = (case when attribute_id = 5 then '$fname'
when attribute_id = 6 then '$mname'
when attribute_id = 7 then '$lname'
end)
where entity_id='$entityId'

此查询工作异常,它确实插入了值为 5、6、7 的 attribute_id 值,但我有更多 attribute_id,每个 attribute_id 在“值”列中都有不同的值。当我触发上述查询时,它会使与所有其他 attribute_id = NULL 相对应的所有值。我不想那样!我希望所有其他 attribute_id 值保持不变。我如何实现这一目标?

最佳答案

case 语句中使用 else 语句:

UPDATE customer_entity_varchar
SET value = (case when attribute_id = 5 then '$fname'
when attribute_id = 6 then '$mname'
when attribute_id = 7 then '$lname'
else value
end)
where entity_id='$entityId'
<小时/>

或者您可以使用 where 标准。

UPDATE customer_entity_varchar
SET value = (case when attribute_id = 5 then '$fname'
when attribute_id = 6 then '$mname'
when attribute_id = 7 then '$lname'
end)
where entity_id='$entityId' and attribute_id in (5,6,7)

关于mysql - 更新查询在同一 id 未提及的行中插入 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37973454/

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