Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
We don't bite newbies here... much
 
PerlMonks  

joining two tables in mysql

by rsiedl (Friar)
on May 15, 2006 at 05:05 UTC ( [id://549407]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

rsiedl has asked for the wisdom of the Perl Monks concerning the following question:

hi monks,

i've written the following code which basically joins two the data from two mysql tables based on an id in the first table.
my $join = 'customer_id'; my $sth = $global{DBH}->prepare(" select * from voice_calls ") +; my $cth = $global{DBH}->prepare(" select * from customers wher +e $join=?"); $sth->execute(); while ( my $call = $sth->fetchrow_hashref() ) { $cth->execute( ${$call}{$join} ); my $customer = $cth->fetchrow_hashref(); $cth->finish; foreach my $key (keys %$customer) { ${$call}{$key} = ${$customer}{$key} } # end-foreach push(@{$self->{RESULTS}}, \%{$call}); } # end-while $sth->finish;
i'm pretty sure there is a way to do this straight from a select in mysql and was wondering if anybody knew it?

cheers,
reagen

Replies are listed 'Best First'.
Re: joining two tables in mysql
by Corion (Patriarch) on May 15, 2006 at 05:13 UTC

    You want the SQL JOIN command, which does just what your code does, except in SQL, and returns the joined results.

    select * from voice_calls left join customers on customers.customer_id = voice_calls.customer_id

    The statement doesn't return any calls for which no customer was found, so make sure you have proper constraints on your voice_calls table, so that every call has a valid customer_id enforced.

    I'm not really sure where your question touches Perl though - a Google search for SQL JOIN directly brings up relevant pages.

Re: joining two tables in mysql
by tinita (Parson) on May 15, 2006 at 05:15 UTC
    this should get you started:
    SELECT * FROM voice_calls INNER JOIN customers ON customers.id = voice_calls.customer_id
    (note that mysql will not prefix the result column names with the table name.)

    by the way, searching the mysql-docs is easy, in most cases you can just type dev.mysql.com/your_search_keyword, so
    http://dev.mysql.com/join leads to
    http://dev.mysql.com/doc/refman/5.0/en/join.html

Re: joining two tables in mysql
by UnderMine (Friar) on May 15, 2006 at 05:59 UTC

    There are two forms of join syntax. The first is the JOIN command explained above. The other is to use the where clause

    SELECT * FROM voice_calls , customers WHERE customers.id = voice_calls.customer_id

    In order to get unique column names you will need to look up aliasing in the SQL manuals. MySQL SELECT Syntax

    Hope this helps

    UnderMine

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://549407]
Approved by Corion
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.