Mysql Join two columns in two different tables

by Rekha 2010-10-08 17:17:54

Mysql Join two columns in two different tables

In case, suppose you have two tables in which you want to sum two column values in two different tables use query as follows,

Example:
CREATE TABLE test (id int(5),text varchar(10));
CREATE TABLE test1 (id int(5),text varchar(10));

insert into test values(1,'test');
insert into test values(2,'test1');
insert into test values(3,'test');

insert into test values(1,'test');
insert into test values(2,'test1');


Suppose if you want to count rows having unique values in both tables and sum them,You may want to do the following:


Select Count(*)
From
(
Select text
From test
Union All
Select text
From test1
) As A
group by text;


Your result will be:

3
2

The first and third row in first table and first row in second table having unique values have been sumed up and result is shown as 3.The second row in first table and second row in second table having unique values have been sumed up and result is shown as 2.

Hope it will be useful to some people.

Tagged in:

1833
like
0
dislike
0
mail
flag

You must LOGIN to add comments