UPDATE clinical_visit_locations JOIN clinical_visits ON clinical_visits.id = clinical_visit_locations.visit_id SET clinical_visits.admitted = 0, clinical_visit_locations.admitted = 0 WHERE clinical_visits.id < 400000;

UPDATE clinical_notes SET clinical_notes.visit_id = (SELECT clinical_visit_locations.visit_id FROM clinical_visit_locations WHERE id = clinical_notes.visit_location_id) WHERE clinical_notes.visit_id = 0;
UPDATE clinical_diagnoses SET clinical_diagnoses.visit_id = (SELECT clinical_visit_locations.visit_id FROM clinical_visit_locations WHERE id = clinical_diagnoses.visit_location_id) WHERE clinical_diagnoses.visit_id = 0;
UPDATE clinical_prescriptions SET clinical_prescriptions.visit_id = (SELECT clinical_visit_locations.visit_id FROM clinical_visit_locations WHERE id = clinical_prescriptions.visit_location_id) WHERE clinical_prescriptions.visit_id = 0;
UPDATE clinical_laboratory SET clinical_laboratory.visit_id = (SELECT clinical_visit_locations.visit_id FROM clinical_visit_locations WHERE id = clinical_laboratory.visit_location_id) WHERE clinical_laboratory.visit_id = 0;
UPDATE clinical_radiology SET clinical_radiology.visit_id = (SELECT clinical_visit_locations.visit_id FROM clinical_visit_locations WHERE id = clinical_radiology.visit_location_id) WHERE clinical_radiology.visit_id = 0;
UPDATE clinical_procedures SET clinical_procedures.visit_id = (SELECT clinical_visit_locations.visit_id FROM clinical_visit_locations WHERE id = clinical_procedures.visit_location_id) WHERE clinical_procedures.visit_id = 0;