mysql - 基于json select查询更新多个表列值

标签 mysql sql json

我刚刚在我的数据库中创建了 4 个新列,分别命名为 cea_nodistrictproperty_typelisting_type。我想根据我的选择查询将它们的结果插入到我添加的新列中。选择查询结果来自行 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;

最佳答案

您需要使用INNER JOIN,而不是CROSS JOIN,否则您将插入不正确的数据。您需要在适当的条件下加入,即 id 值匹配。这应该有效:

update xp_guru_listings x
join (
    SELECT id,
           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) j ON j.id = x.id
set x.cea_no = j.cea_no, 
    x.district = j.district, 
    x.property_type = j.property_type, 
    x.listing_type = j.listing_type;

请注意,您可以直接在 UPDATESET 部分使用 JSON_EXTRACT 公式更简单地编写此代码:

UPDATE xp_guru_listings
SET cea_no = JSON_EXTRACT(json, '$.agencyLicense'),
    district = JSON_EXTRACT(json, '$.district'),
    property_type = JSON_EXTRACT(json, '$.details."Type"'),
    listing_type = RIGHT(JSON_EXTRACT(json, '$.details."Type"'),9)

关于mysql - 基于json select查询更新多个表列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59277919/

相关文章:

mysql - 棘手的 SQL 连接查询

mysql - 如何检查带有 max、count 等 sql 函数的查询返回 NULL?

php - 无法插入 SQL 表或显示全部来自

MySQL 关于排序大小和效率

java - 如何通过Key获取动态JSON值而不解析为Java对象?

php - 检查用户代理并在 session 类中重新生成 session ID

MySQl 我需要使一列仅对于另一列的某些值唯一

java - 如果子查询的结果集列值为 NULL,则显示某个值

javascript - 谁能帮我简化一下?

json - 访问 JSON 响应