use DBI; use Data::Dumper; my $dbh = DBI->connect( 'DBI:mysql:database=test;host=localhost', 'test', 'test', { RaiseError => 1 } ); sub init { # $dbh->do("DROP TABLE t;"); $dbh->do("CREATE TABLE t (id INT, x INT, name CHAR(2));"); $dbh->do("INSERT INTO t (id, x, name) VALUES (1, 0, 'a'), (2, 1, 'b'), (3, 1, 'c'), (4, 2, 'd');"); } sub doit { my ($cols) = @_; my $COLS = join(', ', @$cols); my $sql = "SELECT $COLS FROM t AS a, t AS b WHERE a.name = 'b' AND b.name = 'c' AND a.x = b.x"; my $sth = $dbh->prepare($sql); my $rv = $sth->execute(); my $results = $sth->fetchall_arrayref({}); print Data::Dumper->new([ $sql, $results ], [qw( $sql $results)])->Dump(), "\n"; } init(); doit(['*']); doit(['a.*', 'b.*']); doit(['a.id', 'a.x', 'a.name', 'b.id', 'b.x', 'b.name']); 1;