gpt4 book ai didi

mysql - JSON_ARRAY_APPEND() 如果目标值为空则拒绝 Mysql

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

这里是场景,将用户 ID 添加到 user_details 中。如果 external_id 不存在于配置文件表中并且返回 null(select 语句返回 null)怎么办?

JSON_ARRAY_APPEND 如何拒绝查询并且不添加到 user_details JSON 列。

UPDATE column1 SET user_details= JSON_SET(user_details, "$.ids", IFNULL(user_details->'$.ids',JSON_ARRAY())),
user_details= JSON_ARRAY_APPEND(user_details, "$.ids", (Select id from column2 where external_id='999999999999999'))
where id = 880;

谢谢

最佳答案

UPDATE 语句的 WHERE 子句中检查这一点。

UPDATE column1 SET user_details= JSON_SET(user_details, "$.ids", IFNULL(user_details->'$.ids',JSON_ARRAY())),
user_details= JSON_ARRAY_APPEND(user_details, "$.ids", (Select id from column2 where external_id='999999999999999'))
where id = 880
AND EXISTS (Select id from column2 where external_id='999999999999999')

或者使用JOIN而不是嵌套子查询。

UPDATE column1 AS c1
CROSS JOIN column2 AS c2
SET user_details= JSON_SET(c1.user_details, "$.ids", IFNULL(c1.user_details->'$.ids',JSON_ARRAY())),
c1.user_details= JSON_ARRAY_APPEND(c1.user_details, "$.ids",c2.id)
WHERE c1..id = 880
AND c2.external_id = '999999999999999'

关于mysql - JSON_ARRAY_APPEND() 如果目标值为空则拒绝 Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59534964/

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