gpt4 book ai didi

MySQL - 有条件地构建 JSON_OBJECT

转载 作者:行者123 更新时间:2023-11-30 21:57:04 24 4
gpt4 key购买 nike

是否可以有条件地构建一个 json 对象,例如:

mysql> SELECT JSON_OBJECT(
CASE
WHEN some_condition THEN key, val
ELSE
// do nothing
END CASE

);

如果没有,我如何有条件地构建一个 json 对象。基本上我只想包含非空属性。

最佳答案

一个选项是:

mysql> SET @`key` = 'key',
-> @`val` = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT
-> CASE WHEN @`key` IS NOT NULL AND @`val` IS NOT NULL THEN
-> JSON_OBJECT(@`key`, @`val`)
-> END `json`;
+------------+
| json |
+------------+
| {"key": 1} |
+------------+
1 row in set (0.00 sec)

mysql> SET @`key` = 'key',
-> @`val` = NULL;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT
-> CASE WHEN @`key` IS NOT NULL AND @`val` IS NOT NULL THEN
-> JSON_OBJECT(@`key`, @`val`)
-> END `json`;
+------+
| json |
+------+
| NULL |
+------+
1 row in set (0.01 sec)

关于MySQL - 有条件地构建 JSON_OBJECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44791167/

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