SELECT b2s.book AS book, 1 AS prioritycol FROM book2subjects AS b2s INNER JOIN sub_subject AS ss ON ss.id = b2s.sub_subject INNER JOIN `subject` AS s ON s.id = ss.subject WHERE s.id = 61 UNION SELECT b2s.book AS book, 2 AS prioritycol FROM book2subjects AS b2s INNER JOIN sub_sub_subject AS sss ON sss.id = b2s.sub_sub_subject INNER JOIN sub_subject AS ss ON ss.id = sss.sub_subject INNER JOIN `subject` AS s ON s.id = ss.subject WHERE s.id = 61 ORDER BY prioritycol, RAND() LIMIT 12