• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

perl / SQL how to get value of alias table names?

Ok a quickie... I couldn't find the answer anywhere.

In perl lets say I do a SQL statement using DBI:

$query = 'select m.reg_id, m2.reg_id from table1 m, table2 m2 on m.reg_id = m2.reg_id';

$sth = $dbh->prepare($query);
$sth->execute($board_id);

while ($ref = $sth->fetchrow_hashref())
{
  print $$ref{'m.reg_id'};
  print $$ref{'m2.reg_id'};
}

My quesiton is with out binding the parameters, is it possible to get the values?  $$ref{'m.reg_id'} returns nothing....  $$ref{'reg_id'} does actually return something.  In this case m.reg_id is equal to m2.reg_id, but i'm doing a left join in my actual SQL query and I need to get these values.

Hope this makes sense :/.
0
joeshin
Asked:
joeshin
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you have to give each column name a different alias name.

$query = 'select m.reg_id, m2.reg_id as reg_id_2 from table1 m, table2 m2 on m.reg_id = m2.reg_id';

$sth = $dbh->prepare($query);
$sth->execute($board_id);

while ($ref = $sth->fetchrow_hashref())
{
  print $$ref{'reg_id'};
  print $$ref{'reg_id_2'};
}


now, in general, for the reg_id field which is here joined, you don't need to query it twice, as it HAS to be the same value anyhow.
0
 
ahoffmannCommented:
> $sth->execute($board_id);
I guess that this will return nothing except $board_id is an empty string
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now