SELECT t.nickname addressName, t.packages, t.priority, t.id, t.clientFk, t.companyFk, t.routeFk, if(a.phone, a.phone, c.phone) AS phone, if(a.mobile, a.mobile, c.mobile) AS mobile, wh.name warehouseName, a.city, a.street, a.postalCode, LPAD(a.id, 5, '0') AS addressFk, p.name province, 0 AS import, am.name ticketAgency, tob.description, s.shipFk, u.nickName salesPersonName FROM route r LEFT JOIN ticket t ON t.routeFk = r.id LEFT JOIN address a ON a.id = t.addressFk LEFT JOIN client c ON c.id = t.clientFk LEFT JOIN worker w ON w.id = client_getSalesPerson(t.clientFk, CURDATE()) LEFT JOIN account.user u ON u.id = w.userFk LEFT JOIN ticketObservation tob ON tob.ticketFk = t.id AND tob.observationTypeFk = 3 LEFT JOIN province p ON a.provinceFk = p.id LEFT JOIN warehouse wh ON wh.id = t.warehouseFk LEFT JOIN agencyMode am ON am.id = t.agencyModeFk LEFT JOIN stowaway s ON s.id = t.id WHERE r.id IN(:routesId) ORDER BY t.priority, t.id