Solved

Merging 2 mysql tables

Posted on 2014-02-14
20
335 Views
Last Modified: 2014-03-10
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
Comment
Question by:peps03
  • 9
  • 8
  • 3
20 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39859346
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
 

Author Comment

by:peps03
ID: 39859359
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
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39859385
You just need an index on the Text column in the TableA, for on duplicate key to work
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39859432
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
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39859458
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39859485
@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
 

Author Comment

by:peps03
ID: 39865424
'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
 

Author Comment

by:peps03
ID: 39868069
Is this possible, what i'm trying to accomplish?
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39868104
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
 

Author Comment

by:peps03
ID: 39869906
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39870368
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39870453
@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
 

Author Comment

by:peps03
ID: 39870667
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
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39870784
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
 

Author Comment

by:peps03
ID: 39873186
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
 

Author Comment

by:peps03
ID: 39876525
How can i fix this?
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39876536
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
 

Author Comment

by:peps03
ID: 39883210
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
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39883373
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
 

Author Closing Comment

by:peps03
ID: 39917719
Hi Dan!
Worked great! Thank you very much!!
Thought i already accepted your solution a while ago, sorry for the delay!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now