如何在循环中比较2表

I have this code and i want to display genres table (ID, name) from my database with checkboxes and this works. Then i want to put old values from genres that become from another book_genre(ID,ID_book, ID_genre) table where i keep ids old and this didn't work. In that while i put all genres from my table and then in that input i want to check where is my old genre from my book_genre table and this not work because my loop through 4 times in genres (1.Action, 2. Thriller, SF, Comedy) and in the book_genre he found 2 values (2,3). At first loop check 1Action with 2 and i want to check 1 Action but without nothing from book_genre and i don't know how... .

                $id_book = $_GET['id'];

                $qr_genres_old = 'SELECT genres.name,book_genre.ID_genre,book_genre.ID
                                        FROM genres
                                        INNER JOIN book_genre
                                        ON book_genre.ID_genre = genres.ID
                                        WHERE ID_book = "'. $id_book .'"
                                        ORDER BY ID_genre ASC';
                 $res_genre_old = mysql_query($qr_genres_old);


                 $qr_select_genres = 'SELECT * FROM genres';
                 $res_select_genre = mysql_query($qr_select_genres);


    while( $row_genre_new = mysql_fetch_assoc($res_select_genre) ){ 
                         $row_genre_old = mysql_fetch_assoc($res_genre_old); 

                        ?>

                    <div>
                        <input type="checkbox" name="ID_gen<? echo $id=$row_genre_new['ID']; ?>" 
                                value="1" <? if($row_genre_old['ID_genre']==$row_genre_new['ID'] ) echo 'checked="checked"';  ?> />
                        <? echo $row_genre_new['name']; ?>
                    </div>