how to update one column value which will be copy from another table in sql


How to update one column value which will be copy from another table in sql?

Why i need this.

I have city and state table. The city table showing state name and country name by their id. Due to this, the server side data table, search by state name and country name not working. and same issue with the state table, where search by country name is not working in server side data table. 

To solve this issue, i have created one column country_name in state table and two columns country_name and state_name in city table and filled the data which is copied from state and country table. The below query will show, how i do this.

UPDATE state SET country_name = ( SELECT name FROM country WHERE state.country_id = country.id); 

UPDATE city SET country_name = ( SELECT name FROM country WHERE city.country_id = country.id); 

UPDATE city SET state_name = ( SELECT name FROM state WHERE city.state_id = state.id); 





Comments