• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

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!!
0
peps03
Asked:
peps03
  • 9
  • 8
  • 3
1 Solution
 
Dan CraciunIT ConsultantCommented:
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
0
 
peps03Author Commented:
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?
0
 
Dan CraciunIT ConsultantCommented:
You just need an index on the Text column in the TableA, for on duplicate key to work
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
Ray PaseurCommented:
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...
0
 
Dan CraciunIT ConsultantCommented:
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
0
 
Ray PaseurCommented:
@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.
0
 
peps03Author Commented:
'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..
0
 
peps03Author Commented:
Is this possible, what i'm trying to accomplish?
0
 
Dan CraciunIT ConsultantCommented:
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
0
 
peps03Author Commented:
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?
0
 
Dan CraciunIT ConsultantCommented:
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.
0
 
Ray PaseurCommented:
@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.
0
 
peps03Author Commented:
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...
0
 
Dan CraciunIT ConsultantCommented:
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

0
 
peps03Author Commented:
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)
0
 
peps03Author Commented:
How can i fix this?
0
 
Dan CraciunIT ConsultantCommented:
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.
0
 
peps03Author Commented:
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?
0
 
Dan CraciunIT ConsultantCommented:
OK, I had a bit of time and actually tested :)

So, you do not need any index. Just run

INSERT INTO TableA (Text, Type) 
    SELECT Text, Type FROM TableB

Open in new window

This will add all rows from tableB into tableA.

Afterwards, run:

DELETE FROM TableA WHERE  `Type` =0 AND  `id` NOT IN (
    SELECT * FROM (
         SELECT  `id` 
         FROM TableA
         GROUP BY  `Text` 
         HAVING COUNT(  `Text` ) =1
    ) AS t
)

Open in new window

This will delete all the rows where you had 'Text' values inserted from TableB.

The reason the previous query did not work is because of a MySQL limitation: you can't delete from a table and reference the same table in a subquery.
The solution: use HAVING or a temp table.

Tested all in MySQL 5.6.

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 8
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now