Link to home
Start Free TrialLog in
Avatar of peps03
peps03

asked on

Merging 2 mysql tables

Hi,

I have 2 tables in a mysql database, both with 3 columns each.
ID | Text | Type

Table A is the main table, but all it's Type's are set to 0

Table B should be merged with Table A. A check should be made if table B's text field exists in Table A, if no, add the row (text and type value).
If the text from table B already does exist in Table A, only add/update the Type value to the corresponding row of table A.

How would i do this?

Thank you!!
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Create a unique index on `Text`.

Then run:
INSERT INTO TableA (Text, Type) VALUES (
    SELECT Text, Type FROM TableB)
    ON DUPLICATE KEY UPDATE TableA.Type = TableB.Type

Open in new window

HTH,
Dan
Avatar of peps03
peps03

ASKER

Thanks for your reply Dan!

"Create a unique index on `Text'"":
On both tables?

and should i run this query in the sql section of table a of b?
You just need an index on the Text column in the TableA, for on duplicate key to work
The UNIQUE index will work if all of the Text columns in all of the rows are, in fact, UNIQUE.  If that is not guaranteed, you might want to do a little more processing with a SELECT / UPDATE loop.  Just a thought...
If the rows are not unique then you end up with logic problems: if tableB's text exists in 2 or more rows of tableA's text then what do you do:
- update the first occurrence: how do you define the first occurence? It's the one with the lower id?
- update all occurrences?

BTW, the syntax is:
INSERT INTO TableA (Text, Type) 
    SELECT Text, Type FROM TableB
    ON DUPLICATE KEY UPDATE TableA.Type = TableB.Type

Open in new window

VALUES is not needed
@Dan: I agree it's an issue, but I don't think it's a logic problem, just a matter of understanding how the author wants to apply the business rules.  Without the test data or explanation of the business rules, it's not easy for us to supply a certain solution.  If our author wants to show us the SSCCE we can probably nail down a proof-of-concept code example.
Avatar of peps03

ASKER

'text' in table be is not always UNIQUE. But 'text' in tabel B in combination with the 'Type' value is. (So a text in Table B can occur more then once, but will then always have a different 'Type' value.)

This is also the way it should be entered in table A.

So, if this text is in Table A:
"I like apples" type = 0 (as all types in table A are still 0)

And table B contains:
"I like apples" type = 2
"I like apples" type = 4
"I like apples" type = 7

Table A should become:
"I like apples" type = 2
"I like apples" type = 4
"I like apples" type = 7

Either table A's duplicate text row can be delete or updated.

Hopefully my explanation is clear..
Avatar of peps03

ASKER

Is this possible, what i'm trying to accomplish?
Yes, it's the same answer, only the unique key is no longer `Text`, it's `Text`+`Type`:

ALTER TABLE TableA ADD UNIQUE (`Text`,`Type`);
INSERT INTO TableA (Text, Type) 
    SELECT Text, Type FROM TableB
    ON DUPLICATE KEY UPDATE TableA.Type = TableB.Type

Open in new window


HTH,
Dan
Avatar of peps03

ASKER

When adding Unique to column "Type" of Table A, i get the message:
 #1062 - Duplicate entry '0' for key 'type'

I think because all Type-values are "0" at the moment.

What should i do / am i doing wrong?
Do not add an unique index for "Type".
Add an index for Text and Type.

ALTER TABLE TableA ADD UNIQUE (`Text`,`Type`); means add an unique index where the combination of text and type is unique, regardless of if Text or Type are unique.
@Dan, I think peps03 may have this sort of thing in his TableA:

"I like apples" type = 0
"I like grapes" type = 0
"I like apples" type = 0

That's just a guess, but it's one way that the #1062 could appear.
Avatar of peps03

ASKER

Yes, well, not really these entries shown below, as 1 and 3 are alike.

"I like apples" type = 0
"I like grapes" type = 0
"I like apples" type = 0

But rather:

"I like melons" type = 0
"I like grapes" type = 0
"I like apples" type = 0

I hope i dropped UNIQUE, by running:
alter Table A drop index Text;
(the highlight on UNIQUE in the table structure overview disappeared...)

I ran the query:

ALTER TABLE TableA ADD UNIQUE (`Text`,`Type`);
INSERT INTO TableA (Text, Type)
    SELECT Text, Type FROM TableB
    ON DUPLICATE KEY UPDATE TableA.Type = TableB.Type

But the existing rows in table A that matched Table B's didn't get updated, but were also added.

So where:
"I like apples" type = 0
had to be updated to: type = 3
It got added.

What did i do wrong?

A made a backup, so i can try again...
That's the expected behavior.
"I like apples"+type = 0 is an unique value, not present in TableB, so it was not updated.

You now have all the TableA's items, plus the TableB's items, correct?

And you only want to keep with "type=0" the items that were not in TableB?

To see all the items that were not in TableB, and should remain with `Type`= 0,  you can do:
SELECT`id`, `Text, `Type` FROM TableA
        GROUP BY `Text` HAVING COUNT(`Text`) = 1

Open in new window


To delete the 0 values for the `Text` values that were in TableB, you could do:
DELETE FROM TableA 
    WHERE `Type` = 0 AND `id` NOT IN (
        SELECT`id`, `Text, `Type` FROM TableA
            GROUP BY `Text` HAVING COUNT(`Text`) = 1)

Open in new window

Avatar of peps03

ASKER

You now have all the TableA's items, plus the TableB's items, correct?
> Yes

And you only want to keep the "type=0" items that were not in TableB?
> Yes

Running the second query i get error:
#1241 - Operand should contain 1 column(s)
Avatar of peps03

ASKER

How can i fix this?
DELETE FROM TableA 
    WHERE `Type` = 0 AND `id` NOT IN (
        SELECT`id` FROM TableA
            GROUP BY `Text` HAVING COUNT(`Text`) = 1)

Open in new window


Sorry, the IN operator expected only 1 column.
Avatar of peps03

ASKER

Hi Dan Craciun

#1093 - You can't specify target table 'TableA' for update in FROM clause

To solve this, I thought maybe the second TableA, should be TableB, so i tried that, but then mysql keep "thinking" or "loading" for ever.
Was this correct for me to think?

Do you have a working fix?
ASKER CERTIFIED SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peps03

ASKER

Hi Dan!
Worked great! Thank you very much!!
Thought i already accepted your solution a while ago, sorry for the delay!