$connectsql = qq{SELECT b.service_id, d.stop_a, d.stop_b FROM bus_stops a, routes b, service c, topology d WHERE a.stop_reference = c.stop_reference AND c.service_id = b.service_id AND a.stop_reference = d.stop_a ORDER BY a.stop_reference}; $sth = $dbh->prepare( $connectsql ); $sth->execute(); $sth->bind_columns( undef, \$connect_service, \$connect_stop, \$service_stop ); while( $sth->fetch() ) { print..... } service_id | stop_a | stop_b ------------+------------+------------ 3 | 1010101010 | 8888888888 1 | 3333333333 | 6666666666 2 | 6666666666 | 3333333333 2 | 8888888888 | 1010101010