SELECT r.*, IF( p.AddressId = 0, r.AddressId, p.AddressId ) as AddressId, p.PersonId,p.PositionId, IFNULL(type.Title,'') as TypeTitle, IFNULL(type.TitleLat,'') as TypeTitleLat, IFNULL(type.Color,'') as TypeColor, get_fulladdr_by_id( IF( p.AddressId = 0, r.AddressId, p.AddressId ) ) as AddrJson FROM `edu_uch` as r INNER JOIN `edu_uch_person` as p ON(r.Id = p.UchId AND p.Main = 1) LEFT JOIN edu_uch_type as type ON(type.Id = r.TypeId) WHERE p.PersonId = 192404 LIMIT 100