DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticketDown_PrintableSelection`(vSectorFk INT) BEGIN UPDATE vn.ticketDown td JOIN (SELECT DISTINCT t.id FROM vn.ticket t JOIN vn.sale s ON s.ticketFk = t.id JOIN vn.itemShelvingSale iss ON iss.saleFk = s.id JOIN vn.itemShelving ish ON ish.id = iss.itemShelvingFk JOIN vn.shelving sh ON sh.id = ish.shelvingFk JOIN vn.parking p ON p.id = sh.parkingFk WHERE p.sectorFk = vSectorFk ) sub ON sub.id = td.ticketFk JOIN vn.ticketDown_SelectionType tdst ON tdst.description = 'FREE' JOIN vn.ticketDown_SelectionType tdst2 ON tdst2.description = 'SELECTED' SET td.selected = tdst2.id WHERE td.selected = tdst.id; END$$ DELIMITER ;