CREATE OR REPLACE VIEW `v_visits_detail`  AS  
select `visits`.`id` AS `id`,`visits`.`place_id` AS `place_id`,`places`.`name` AS `place_name`, 
`institutions`.`id` AS `institution_id`, 
`institutions`.`name` AS `institution_name`, `institutions`.`city_id` AS `city_id`, `cities`.`name` AS `city_name`, `visits`.`program_id` AS `program_id`, 
`programs`.`name` AS `program_name`,`visits`.`user_id` AS `user_id`,`users`.`name` AS `user_name`, 
`visits`.`visit_type_id` AS `visit_type_id`,`visit_types`.`name` AS `visit_type_name`,`visits`.`date` AS `date`,
`visits`.`contact` AS `contact`,`visits`.`contact_phone` AS `contact_phone`,`visits`.`type` AS `type`,
`visits`.`status` AS `status` , `visits`.`observations` AS `observations`, 
(case `visits`.`status` 
when 0 then 'Cancelada'  
when 1 then 'Aprobada' 
when 2 then 'Pendiente' 
when 3 then 'Completa' 
else '' 
end) as 'estado'
from `visits` 
join `places` on `places`.`id` = `visits`.`place_id` 
join `institutions` on `institutions`.`id` = `places`.`institution_id` 
join `cities` on `cities`.`id` = `institutions`.`city_id`
join `programs` on `programs`.`id` = `visits`.`program_id` 
join `users` on `users`.`id` = `visits`.`user_id` 
join `visit_types` on `visit_types`.`id` = `visits`.`visit_type_id`;