I just created 4 new columns in my database named cea_no, district, property_type and listing_type. I want to insert the result of them based on my select query into the new column that i added. The select query result is from the row json and it is extracted from a json data. How could I achieve that? I tried some methods and it worked, the problem is it inserts a new row and my data is now doubled.
My table structure.
+------------------+------------+------+-----+---------------------+-------------------------------+
| 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() |
+------------------+------------+------+-----+---------------------+-------------------------------+
The query that I tried
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;
Sample result which is correct
+------------------------------+----------+------------------------+--------------+
| 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" |
+------------------------------+----------+------------------------+--------------+
That is the values that I want to insert in the new columns.
EDIT: I tried this query but it wont work
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;