SELECT inst.* from dir_courses AS cTable JOIN dir_inst_tut_courses AS instc ON cTable.table_id= instc.course_table_id JOIN dir_institutes AS inst ON inst.table_id= instc.inst_table_id WHERE cTable.table_id='507' AND inst.inst_city_id='501' GROUP BY inst.table_id ORDER BY rand() LIMIT 5