DROP procedure IF EXISTS `vn`.`itemLastEntries`; DELIMITER $$ CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`itemLastEntries`(IN `vItem` INT, IN `vDays` DATE) BEGIN SELECT w.id AS warehouseFk, w.name AS warehouse, tr.landed, b.entryFk, b.isIgnored, b.price2, b.price3, b.stickers, b.packing, b.`grouping`, b.groupingMode, b.weight, i.stems, b.quantity, b.buyingValue, b.packageFk , s.id AS supplierFk, s.name AS supplier FROM itemType it RIGHT JOIN (entry e LEFT JOIN supplier s ON s.id = e.supplierFk RIGHT JOIN buy b ON b.entryFk = e.id LEFT JOIN item i ON i.id = b.itemFk LEFT JOIN ink ON ink.id = i.inkFk LEFT JOIN travel tr ON tr.id = e.travelFk LEFT JOIN warehouse w ON w.id = tr.warehouseInFk LEFT JOIN origin o ON o.id = i.originFk ) ON it.id = i.typeFk LEFT JOIN edi.ekt ek ON b.ektFk = ek.id WHERE b.itemFk = vItem And tr.shipped BETWEEN vDays AND DATE_ADD(CURDATE(), INTERVAl + 10 DAY) ORDER BY tr.landed DESC , b.id DESC; END$$ DELIMITER ;