社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

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

user001232 • 5 年前 • 1653 次点击  

我刚在数据库中创建了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;
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51097
 
1653 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Vince
Reply   •   1 楼
Vince    5 年前

实际上,你的问题是正确的。您只需要在子查询中添加别名,子查询才能正常工作。就像这样

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;
Nick
Reply   •   2 楼
Nick    5 年前

你需要使用 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;

注意,您可以使用 JSON_EXTRACT 公式直接在 SET 部分 UPDATE :

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)