Alex Banerjee
Alex Banerjee

Reputation: 474

Checking data before importing mysql

I have a table full of data in "businesses", and a new table called "table 9" which has come in through an import. I'm trying to migrate the table 9 data into businesses but i need to make sure it doesn't already exist. I have no choice but to run this live (i have a backup)

This is my code so far :

INSERT INTO businesses

(Business, Address1,Address2,Address3,Town,Postcode,BusinessType,Contact,Position,Telephone)

SELECT Company,

line1,

line2,

line3,

town,

postcode,

trade,

(SELECT CONCAT(`ContactSalutation`, ' ', `ContactFirstName`, ' ', `ContactLastName`) FROM telesales.`table 9`),

ContactPosition,

phoneno pnum

 FROM telesales.`table 9` ts

 where pnum NOT IN (SELECT DISTINCT Telephone

                         FROM businesses

                         WHERE Telephone = pnum)

Firstly is this going to do what i expect? only insert if the phone number in table 9 doesnt exist in businesses table, and is there a way to say if it does exist then update it instead with the new data?

Upvotes: 2

Views: 156

Answers (3)

Neville Kuyt
Neville Kuyt

Reputation: 29649

Firstly,

(SELECT CONCAT(`ContactSalutation`, ' ', `ContactFirstName`, ' ', `ContactLastName`) FROM telesales.`table 9`),

is almost certainly not what you want. You're saying "get all this information from table 9 and put it into businesses", without a join or a where clause. Not sure how MySQL reacts to this, but the best case is that you get a random row, not the row associated with the one you're inserting. I think you want to replace it with:

CONCAT(`ContactSalutation`, ' ', `ContactFirstName`, ' ', `ContactLastName`),

Secondly, this query is highly dependent on the formatting of phone numbers. That's probably not a good idea, unless you can guarantee that formatting is consistent.

For instance is "00 31 20 787 9000" the same phone number as "00 31 207 87 9000"?

In cases like this, I typically create an additional column on table9 (e.g. "businessID"), and write queries to populate that.

For instance:

update table9 t9
inner join  businesses b
  on  b.phone = t9.phone
set businessID = b.businessID

Sense check the table; you may find you then need to do

update table9 t9
inner join  businesses b
  on  b.phone = trim(t9.phone)
set businessID = b.businessID

Finally, you can insert the records from table9 into businesses that have a null businessID, and update the others.

Upvotes: 1

e4c5
e4c5

Reputation: 53774

REPLACE does work but ..

REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.

But that means a great deal more of time and effort will be spent by the database on updating the indexes. Better would be to use INSERT IGNORE or INSERT .. ON DUPLICATE KEY

Upvotes: 1

Jakumi
Jakumi

Reputation: 8374

if "if it exists" means, it has the same primary key, then yes, you should use REPLACE instead of INSERT. See http://dev.mysql.com/doc/refman/5.5/en/replace.html

However, you should probably create a copy of your target table first ;o)

Upvotes: 1

Related Questions