I have 3 tables: SITES, DEVICES AND LOADS.
- SITES columns are (ID, default_MGR_ID);
- DEVICES columns are (ID, site_id (which is the foreign key of sites))
- LOADS columns are (ID, device_id (foreign key of devices), site_mgr_id)
I want to update LOADS.site_mgr_id to be equal to SITES.default_mgr_id.
Can you help me write a SQL query to do that?
This is the query I have so far but is not working:
UPDATE loads SET loads.site_mgr_id =(SELECT default_site_mgr_id FROM sites s WHERE s.id =(SELECT site_id FROM devices d WHERE d.id = loads.device_id)
2
Answers
Using the initial question’s field naming notation (You can change to what your field names are); try this:
update with subselect, must be guaranteed that returns just one row;
this is not an optimized query, but just for a quick answer, you can use one of the following two methods :
or use this :