Do you have the option of redesigning the tables? The old trick of putting an array into a database field will always cause this kind of headache.
If you can't use the dbms the way it should be used, just treat the tables like a text file (obviously this was designed by someone who was thinking of files rather that databases). Read a row (line from the file) look for the link in the other table (file).