Update

With the addition of the code I can see exatly what you mean. Similar to my inelegant hack below. Thanks++

I may be being obtuse but I don't quite see how. Essentially we have to 'join' the two tables so we can sum the counts (ie b occurs with a count of 1 in each of the original tables so need to have a count of 2 in the final table, whereas a and c retain their counts of 1)

With a merge we will just get 2 rows of b.

This works (it is a kind of a merge) but is not very efficient:

mysql> select * from tok1; +-------+-------+ | token | count | +-------+-------+ | a | 1 | | b | 2 | +-------+-------+ 2 rows in set (0.00 sec) mysql> select * from tok2; +-------+-------+ | token | count | +-------+-------+ | b | 1 | | c | 4 | +-------+-------+ 2 rows in set (0.00 sec) mysql> insert into all_tok select token,count,0 from tok1 union selec +t token,0,count from tok2; Query OK, 4 rows affected (0.01 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from all_tok; +-------+--------+--------+ | token | count1 | count2 | +-------+--------+--------+ | a | 1 | 0 | | b | 2 | 0 | | b | 0 | 1 | | c | 0 | 4 | +-------+--------+--------+ 4 rows in set (0.00 sec) mysql> select token,sum(count1+count2) from all_tok group by token; +-------+--------------------+ | token | sum(count1+count2) | +-------+--------------------+ | a | 1 | | b | 3 | | c | 4 | +-------+--------------------+ 3 rows in set (0.00 sec) mysql>

We need the two columns for count so we have b,0,N and b,N,0 so the union does not obliterate one of the b's.

cheers

tachyon


In reply to Re: Re: OT: MySQL combine 2 tables data when Perl 'fails' by tachyon
in thread OT: MySQL combine 2 tables data when Perl 'fails' by tachyon

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.