----- table A: id int(11) auto_increment, name varchar(25) ----- table B: a_id int(11), b_num int(11) ----- can I optimize this? SELECT a_id FROM B WHERE b_num = '31337'; loop: and based on the result, I fetch name for that ID from table A: SELECT name FROM A WHERE id = $a_id;