Imagine that you have a table like this:
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | | |
| parent_id | int(11) | YES | MUL | NULL | |
+-------------+------------------+------+-----+---------+----------------+
Let’s call this table locations
This represents a city or a state.
For example if name field is Los Angeles, its parent_id would represent a row with a name fieldCalifornia.
Now imagine that you have another table like this:
+-----------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(450) | YES | | NULL | |
| state | varchar(135) | YES | | NULL | |
+-----------------+---------------+------+-----+---------+----------------+
Let’s call this table cities.
Each row represents a city, and the id field matches the idin the locationstable.
In this table the state field is always empty, so I would like to update it with the name field from the locations table.
I’ve tried this query in order to get the state value, but it doesn’t seem to work (it takes a long time and nothing happens):
SELECT name FROM locations WHERE id IN
(SELECT parent_gid FROM locations INNER JOIN cities
ON locations.id = cities.id);
Any suggestions on how to achieve this?
if only only want
SELECTstatement, useJOINbut if you want to update the table,
UPDATE