OK, I'm going to assume a database design like the following. It's rather simplified, but it'll get the idea across.

Actors
------
A_ID           integer
A_Fullname     varchar

Episodes
--------
E_ID           integer
E_Title        varchar

Roles
-----
R_ID           integer
R_Name         varchar

Actor_Role_Ep
-------------
ARE_ID         integer
ARE_Actor_ID   FK References Actor(A_ID)
ARE_Episode_ID FK References Episode(E_ID)
ARE_Role_ID    FK References Role(R_ID)

Now, if you have some actors in multiple roles, or some roles played by multiple actors, your SQL statement will return multiple rows. So you should then loop through the results in a while loop. Example code below.

# I'm assuming a valid $dbh exists.

my $sql = $dbh->prepare_cached("
SELECT ARE_ID, A_Fullname, E_Title, R_Name
FROM   Actor_Role_Ep, Actor, Episode, Role
WHERE  ARE_Actor_ID   = A_ID
AND    ARE_Role_ID    = R_ID
AND    ARE_Episode_ID = E_ID
WHERE  (whatever you want here);");

$sql->execute();
# If you need to pass in information above, then put the variables inside the ()'s.

while (($are_id, $a_name, $e_title, $r_name) = $sql->fetchrow_array) {
  print "$a_name played $r_name in episode $e_title.\n";
}

I hope that helps you..

-Yendor

IWeThey
HOPE Ride


In reply to Re: SQL query: are all results in one hashref key the same? by Yendor
in thread SQL query: are all results in one hashref key the same? by Cody Pendant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.