in reply to OT: MySQL combine 2 tables data when Perl 'fails'

Can you use a merge table?
CREATE TABLE merge_table (table definition) TYPE=MERGE UNION=(table1,t +able2) SELECT tok, COUNT(tok) FROM merge_table GROUP BY tok

Replies are listed 'Best First'.
Re: Re: OT: MySQL combine 2 tables data when Perl 'fails'
by tachyon (Chancellor) on Feb 10, 2004 at 18:37 UTC

    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

      create table test(tok char(1) not null, count int(1) not null, key(tok +)) create table test2(tok char(1) not null, count int(1)not null, key(tok +)) create table test_merge (tok char(1)not null, count int(1) not null, k +ey(tok)) type=merge union=(test,test2) SELECT tok, sum(count) FROM `test_merge` WHERE 1 group by tok