select qg.*,gg.finalgrade,gi.itemname,gi.courseid,gi.itemmodule,gi.itemtype from mymdl_quiz_grades as qg left join mymdl_grade_grades as gg on qg.userid=gg.userid inner join mymdl_grade_items as gi on gg.itemid=gi.id where gi.courseid=2 and qg.userid