SELECT b2s.book AS bookid, 2 AS prioritycol FROM book2subjects AS b2s INNER JOIN sub_sub_subject AS sss ON sss.id = b2s.sub_sub_subject INNER JOIN book ON book.id = b2s.book WHERE sss.id = 786 ORDER BY prioritycol, RAND() LIMIT 12