简体   繁体   English

导入mysql之前检查数据

[英]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. 我有一个充满“业务”中数据的表,还有一个通过导入引入的名为“表9”的新表。 I'm trying to migrate the table 9 data into businesses but i need to make sure it doesn't already exist. 我正在尝试将表9数据迁移到企业中,但是我需要确保它不存在。 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? 仅在表9中的电话号码在业务表中不存在时才插入,是否有办法说出该电话号码是否存在,然后用新数据进行更新?

if "if it exists" means, it has the same primary key, then yes, you should use REPLACE instead of INSERT . 如果“如果存在”表示它具有相同的主键,那么是的,您应该使用REPLACE而不是INSERT See http://dev.mysql.com/doc/refman/5.5/en/replace.html http://dev.mysql.com/doc/refman/5.5/en/replace.html

However, you should probably create a copy of your target table first ;o) 但是,您可能应该首先创建目标表的副本; o)

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. REPLACE的工作方式与INSERT完全相同,不同之处在于,如果表中的旧行与PRIMARY KEY或UNIQUE索引的新行具有相同的值,则在插入新行之前会删除该旧行。

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 更好的方法是在重复键上使用INSERT IGNORE或INSERT ..

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. 您说的是“没有表9的所有信息并将其投入业务”,而没有联接或where子句。 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. 不确定MySQL如何对此做出反应,但是最好的情况是您会得到一个随机行,而不是与您要插入的行关联的行。 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"? 例如,“ 00 31 20 787 9000”与“ 00 31 207 87 9000”是相同的电话号码吗?

In cases like this, I typically create an additional column on table9 (eg "businessID"), and write queries to populate that. 在这种情况下,我通常在table9上创建一个附加列(例如“ businessID”),并编写查询以填充该列。

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. 最后,您可以将table9中的记录插入到具有空businessID的业务中,并更新其他记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM