Mysql 基于json select查询更新多个表列值
我刚刚在数据库中创建了4个新列,分别命名为cea\u no、district、property\u type和listing\u type。我想将基于select查询的结果插入到我添加的新列中。select查询结果来自json行,并从json数据中提取。我怎样才能做到这一点?我尝试了一些方法,效果很好,问题是它插入了一个新行,我的数据现在翻了一倍 我的桌子结构Mysql 基于json select查询更新多个表列值,mysql,sql,json,Mysql,Sql,Json,我刚刚在数据库中创建了4个新列,分别命名为cea\u no、district、property\u type和listing\u type。我想将基于select查询的结果插入到我添加的新列中。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;
实际上,您的查询是正确的。您只需将别名添加到子查询中即可使其正常工作。是这样的
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) as x
set cea_no = cea_no,
district = district,
property_type = property_type,
listing_type = listing_type;
您需要使用内部联接,而不是交叉联接,否则将插入不正确的数据。您需要在适当的条件下加入,即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;
请注意,您可以更简单地在更新的SET部分直接使用JSON_EXTRACT公式编写:
哦,我明白了。这就是为什么它总是返回错误代码:1248。每个派生表都必须有自己的别名。非常感谢,真的吗,先生?为什么他会使用交叉连接得到错误的数据?请向我解释。为什么使用交叉连接sir会得到不正确的数据?因为交叉连接将一个表的每一行连接到另一个表的每一行,所以无法保证派生表中的哪一行数据将用于更新主表我尝试使用您的查询sir,但它在字段列表中返回'Column'cea_no'是不明确的'i just'从更新中删除了xstatement@Vince不需要道歉——好吧,反正也不需要向我道歉!
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)