DROP TABLE IF EXISTS newtable; CREATE TABLE newtable SELECT T.time, T.name, T.last, P.company, P.wife FROM T JOIN P ON T.wife = P.wife and T.company = P.company; #### $sql="DROP TABLE IF EXISTS newtable; CREATE TABLE newtable SELECT T.time, T.name, T.last, P.company, P.wife FROM T JOIN P ON T.wife = P.wife and T.company = P.company"; $sth = $dbh->prepare($sql); $sth->execute (); $sth->finish (); $dbh->disconnect ();