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

Posted on 2007-08-05
Last Modified: 2012-08-13
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);

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 :/.
Question by:joeshin
    LVL 142

    Accepted Solution

    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);

    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.
    LVL 51

    Expert Comment

    > $sth->execute($board_id);
    I guess that this will return nothing except $board_id is an empty string

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now