mysql json链接表_MySQL基于json select查询更新多个表列值

我刚在数据库中创建了4个名为

cea_no

,

district

,

property_type

listing_type

. 我想将基于select查询的结果插入到我添加的新列中。选择查询结果来自行

json

它是从

json格式

数据。我怎么能做到?我尝试了一些方法,结果成功了,问题是它插入了一个新行,我的数据现在翻了一番。提前谢谢

我的桌子结构。

+------------------+------------+------+-----+---------------------+-------------------------------+

| Field | Type | Null | Key | Default | Extra |

+------------------+------------+------+-----+---------------------+-------------------------------+

| id | int(11) | NO | PRI | NULL | auto_increment |

| json | mediumtext | NO | | NULL | |

| property_name | text | NO | | NULL | |

| property_address | text | NO | | NULL | |

| price | text | NO | | NULL | |

| listed_by | text | NO | | NULL | |

| contact | text | NO | | NULL | |

| cea_no | text | NO | | NULL | EMPTY for now |

| district | text | NO | | NULL | EMPTY for now |

| property_type | text | NO | | NULL | EMPTY for now |

| listing_type | text | NO | | NULL | EMPTY for now |

| update_time | timestamp | NO | | current_timestamp() | on update current_timestamp() |

+------------------+------------+------+-----+---------------------+-------------------------------+

我试过的问题

SELECT JSON_EXTRACT(json, '$.agencyLicense') AS cea_no,

JSON_EXTRACT(json, '$.district') AS district,

JSON_EXTRACT(json, '$.details."Type"') AS property_type,

RIGHT(JSON_EXTRACT(json, '$.details."Type"'),9) AS listing_type

from xp_guru_listings;

正确的样本结果

+------------------------------+----------+------------------------+--------------+

| cea_no | district | property_type | listing_type |

+------------------------------+----------+------------------------+--------------+

| "CEA: R017722B \/ L3009740K" | "(D25)" | "Apartment For Sale" | For Sale" |

| "CEA: R016023J \/ L3009793I" | "(D25)" | "Condominium For Sale" | For Sale" |

| "CEA: R011571E \/ L3002382K" | "(D25)" | "Condominium For Sale" | For Sale" |

| "CEA: R054044J \/ L3010738A" | "(D21)" | "Apartment For Sale" | For Sale" |

| "CEA: R041180B \/ L3009250K" | "(D09)" | "Condominium For Sale" | For Sale" |

+------------------------------+----------+------------------------+--------------+

这是我要在新列中插入的值。

编辑:

我试过这个问题但没用

update xp_guru_listings cross join (

SELECT JSON_EXTRACT(json, '$.agencyLicense') AS cea_no,

JSON_EXTRACT(json, '$.district') AS district,

JSON_EXTRACT(json, '$.details."Type"') AS property_type,

RIGHT(JSON_EXTRACT(json, '$.details."Type"'),9) AS listing_type

from xp_guru_listings

)

set cea_no = cea_no,

district = district,

property_type = property_type,

listing_type = listing_type;

在C语言中,为了从MySQL数据库中提取数据并将其封装成JSON包,你需要结合使用`libmysqlclient`库处理MySQL连接和查询,以及`json-c`库来处理JSON格式的数据。以下是大致步骤: 1. **链接库**: 首先,需要包含必要的头文件,并链接到MySQLJSON库: ```c #include <stdio.h> #include <mysql.h> #include <json-c/json.h> ``` 2. **初始化数据库连接**: 使用`mysql_init`和`mysql_real_connect`函数建立到MySQL服务器的连接,提供数据库名、用户名、密码和主机信息。 ```c MYSQL *conn; conn = mysql_init(NULL); if (!mysql_real_connect(conn, "localhost", "username", "password", "database_name", 0, NULL, 0)) { // 处理错误 } ``` 3. **编写SQL查询**: 编写一个SQL查询来获取所需的表数据。 ```c char query[] = "SELECT * FROM table_name"; ``` 4. **执行查询**: 执行查询并获取结果集。 ```c mysql_query(conn, query); MYSQL_RES *res = mysql_store_result(conn); ``` 5. **将结果转换为JSON**: 遍历结果集,将每行数据转化为键对,然后添加到JSON对象中。最后使用`json_object_array_append`或类似函数构建整个JSON数组。 ```c JSON_Object *obj = json_object_new_object(); while (MYSQL_ROW row = mysql_fetch_row(res)) { JSON_Value *value; JSON_Object *item_obj; for (int i = 0; i < mysql_num_fields(res); ++i) { value = json_value_new_string(row[i]); item_obj = json_object_new_from_json_value(value); json_object_object_add(obj, mysql_field_name(res, i), item_obj); json_value_free(value); } } JSON_String *json_str = json_object_to_json_string(obj); ``` 6. **释放资源**: 关闭数据库连接和清理内存。 ```c mysql_free_result(res); mysql_close(conn); // 如果json_str不是NULL,则释放字符串 if (json_str) { json_decref(json_str); } ``` 记得在实际应用中处理可能出现的错误,并且根据需要调整上述示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值