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

Hi, was wondering if anyone can give me advice on how I can carry this out.

two mysql tables
one with a list of names ------------------ bobby angel joey brandy jeff mike ------------------ the other one with also a list of names (this table will include less +names at all times) ------------------ jeff mike ------------------


So if table 1 will always have about 5-15 more names and table 2 will always have way less, only about 3-4 rows or so....how can I join them and print them in the web browser into one list, having any name highlighted in BOLD if it appears in both tables. So the above list will be printed as:

bobby
angel
joey
brandy
jeff
mike


I see one way but it might not be as efficient. The tables will grow in the future so I need to keep this in mind. One way was fetching both tables into arrays, and do a foreach statement on one array and tryin to match it to the values in the other. Am I on the right track with this? Not quite sure...well thanks.

Leo

2006-08-10 Retitled by planetscape, as per Monastery guidelines

( keep:9 edit:18 reap:5 )

Original title: 'Combining two fetches with DBI and MySQL'

2006-08-20 Retitled by Arunbear, not off topic (Keep: 7, Edit: 20, Reap: 1)

Replies are listed 'Best First'.
Re: Combining two fetches with DBI and MySQL
by davorg (Chancellor) on Aug 08, 2006 at 15:33 UTC

    It's easier to do this in SQL and not in Perl.

    You need to do an outer join between the two tables.

    select t1.name, t2.name from t1 left outer join t2 on t1.name = t2.name

    You'll get back a two column result set. If a name is in both tables then it will appear in both columns. If it only appears in t1 then the second column will contain NULL.

    You then need to write the Perl that handles that and creates your list.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      A good solution, but it assumes that table 2 is a subset of table 1. This may be true, but is not explicitly said, So if in the above example table 2 also contained 'brian', then 'brian' would be missing from the output. The addition of a union would fill in any rows in table 2 but not in table 1
      select t1.name, t2.name from t1 left outer join t2 on t1.name = t2.name union select t2.name, t1.name from t2 left outer join t1 on t2.name = t1.name where isnull(t1.name)
Re: Combining two fetches with DBI and MySQL
by rhesa (Vicar) on Aug 08, 2006 at 15:37 UTC
    You're looking for a left join:
    select table1.name as left_name, table2.name as right_name from table1 left join table2 on table1.name=table2.name
    Then you can make the name bold when left_name eq right_name. right_name will be undef if the left_name doesn't occur in table2.

    I suggest you read up on relational databases, foreign keys, natural vs. synthetic keys etc. Your model smells of denormalisation.

Re: Combining two fetches with DBI and MySQL
by blogical (Pilgrim) on Aug 08, 2006 at 16:31 UTC
    Pull the results off each table into arrays. Run a map over both arrays to increment the value of the key that matches the name. Bold if value > 1.
    my @table1 = GetNames( 'Table1'); my @table2 = GetNames( 'Table2'); my %names = (); map {$names{$_}++} (@table1, @table2); while (my ($name, $weight) = each %names) { if ( $weight > 1 ) { PrintBoldly($name); } else { PrintNormally($name); } }

    "One is enough. If you are acquainted with the principle, what do you care for the myriad instances and applications?"
    - Henry David Thoreau, Walden

A reply falls below the community's threshold of quality. You may see it by logging in.