SELECT b2s.book AS bookid, 1 AS prioritycol FROM book2subjects AS b2s INNER JOIN sub_subject AS ss ON ss.id = b2s.sub_subject INNER JOIN book ON book.id = b2s.book WHERE ss.id = 190 UNION 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 sub_subject AS ss ON ss.id = sss.sub_subject INNER JOIN book ON book.id = b2s.book WHERE ss.id = 190 ORDER BY prioritycol, RAND() LIMIT 12