0

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;

2 Answers 2

1

You need to use an INNER JOIN, not a CROSS JOIN otherwise you will get incorrect data inserted. And you need to join on the appropriate condition which is that the id values match. This should work:

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;

Note that you could write this more simply using the JSON_EXTRACT formulas directly in the SET part of the 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)
Sign up to request clarification or add additional context in comments.

14 Comments

Really sir? why would he get incorrect data by using cross join? please explain to me.
Why would i get incorrect data from using cross join sir?
Because a cross join joins every row of one table to every row of the other so there's no guarantee which row's data from derived table will be used to update the main table
I tried using your query sir but it returns ` Column 'cea_no' in field list is ambiguous` I just removed the x from the update statement
@Vince no need to apologise - well, not to me anyway!
|
0

Actually, your query is correct. You just need to add an alias to your subquery in order for it to work. Its like this

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;

1 Comment

Ohh I see. So that why it always returns Error Code: 1248. Every derived table must have its own alias . Thanks a lot for this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.