bigup401:

It looks like you're trying to get the sender and recipient user names for messages and are having trouble with the SQL. You can use the same table multiple times with a different alias each time as shown below. I'm aliasing the USERS table to SND for the sender and to REC for the recipient to get the first and last name of both the sender and recipient for each message:

SELECT M.msg_id, SND.firstname as snd_fname, SND.lastname as snd_lname, REC.firstname as rec_fname, REC.lastname as rec_lname from MSG as M -- We'll use SND as the alias for the sender join USERS as SND on M.msg_from = SND.usrid -- and REC as the alias for the recipient join USERS as REC on M.msg_to = REC.usrid

If you're going to fetch all the information as arrays, you don't actually need to alias the column names, and you could just print it as you were suggesting. But you need to be careful with your variable names. You're trying to use $data_check in two places, and you haven't loaded $datainfo. I've changed it a little:

$data->execute(); my $data_check = $data->fetchall_arrayref(); for my $row (@$data_check) { my ($msgid, $snd_fname, $snd_lname, $rec_fname, $rec_lname) = @$row +; printf "% 6u %-32.32s %-32.32s\n", $msgid, join(" ", $snd_lname, $snd_fname), join(" ", $rec_lname, $rec_fname); }

However, fetching all the data at once might be a problem when you're dealing with large datasets. Also, by fetching the data as arrays, then any change to your select statement will require you to change the code that converts each array slot into an individual variable. For that reason, I normally fetch a row at a time and fetch hashrefs instead of arrays, like this:

$data->execute(); my $data_check = $data->fetchall_arrayref(); while (my $row = $data->fetchrow_hashref) { printf "% 6u %-32.32s %-32.32s\n", $row->{msgid}, join(" ", $row->{snd_lname}, $row->{snd_fname}), join(" ", $row->{rec_lname}, $row->{rec_fname}); }

Here, since I've fetched the data as hashes, I can just use the column aliases as the keys for each row. That way, I don't have to map the columns to variables, instead I can just use the aliased column names, and the order doesn't matter at all.

I hope this helps. I couldn't quite tell exactly what you were asking, so I just tossed out my thoughts. I haven't tested any of the code, so if there are any syntax errors, let me know and I'll update it with corrections.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: output results from sql join by roboticus
in thread output results from sql join by bigup401

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.