tags:

views:

41

answers:

2

Hello all, this is actually a followup from a previous question but it contains different information , so I decided to start a new topic. Summary :

I have 2 tables one TMP_TABLE and a BKP_TABLE. Both have the same fieldstructure and identical datatypes for the fields( with almost identical data).

let's say TMP_TABLE is constructed like this

TMP_TABLE

NAME      PREFIX      PARAMETERS 
data      data        data
data      data        data
data      data        data

and BKP_TABLE looks like this

BKP_TABLE

NAME      PREFIX      PARAMETERS 
data1      data1        data1
data1      data1        data1
data1      data1        data1

Is it possible to combine these two tables into one that looks like this

END_RESULTTABLE 


    NAME      PREFIX      PARAMETERS 
    data      data1        data1
    data      data1        data1
    data      data1        data1

As you can see I wish to drop one of the fields and replace it with another. The sequence is pretty much the same so I don't have to worry about records being incorrect.

A side question

At the moment both TMP and BKP contain the exact same data ( 113 records) when I do this

SELECT * FROM TMP_TABLE 
UNION ALL 
SELECT * FROM BKP_TABLE 

I get 226. Why does this happen. I thought that duplicate entries ( which I can clearly see) would not appear in my virtual table.

EDIT:

I would like to replace one field of TMP_data with BKP_table field ( example like name).

+2  A: 
UNION ALL  

will return all records from both selects (hence the ALL)

UNION

will remove duplicates

Mark Baker
Thanks for the reply. UNfortunately I tried union before I tried union all. And both give me the same result.
jovany
@jovany, yes, of course: `UNION` removes *duplicates*. If the rows are not completely identical then they are not duplicates.
Jeffrey Kemp
aha of course, so for my eye they probably "look" identical but content wise there is probably a slight difference. Thank you.
jovany
Ok so I was missing my primary key, that was my problem. Thanks
jovany
A: 

Assuming your two tables have a key in common (e.g. name), you can do something like this:

create table end_resulttable as
select t.name, t.prefix, b.parameters
from   tmp_table t, bkp_table b
where  t.name = b.name;

Is that what you mean?

Tony Andrews
I too got that far, unfortunately it doesn't quite do what I want. Because now it multiplies my records so when I do this I get 113 * 113 records in my new end_resulttable. And what I want( if it's possible ) is actually just replace a field of data. I'll make an edit to my post.
jovany
If you got 113*113 rows, i.e. a Cartesian product, that means you didn't specify a join condition ("where t.name = b.name" in my answer) - OR that the column you joined on isn't a key, and has the same value in all 113 rows!
Tony Andrews
Do your tables in fact have keys, or are you just hoping to "line up" row 1 of table A with row 1 of table B and so on? If so, databases DON'T work like that.
Tony Andrews
Thank you!... indeed I did not specify my pk so I got the cartesian product. top
jovany