Solved

Merging 2 mysql tables

Posted on 2014-02-14
20
342 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 109

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 109

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
 
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 109

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Is Dropbox the only way to upload large files? 3 43
How to display div value on input field value in php 11 53
Database Design Dilemma 6 58
Perl Versus AWK? 7 49
Creating and Managing Databases with phpMyAdmin in cPanel.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

840 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