简体   繁体   中英

Check if entry exists and not insert in mysql

I am doing an insert from an imported table and adding data into multiple tables using mysql.

Basically when doing the insert there are some null fields as the data has been imported from a csv.

What I want to do is extract the data and not create multiple null entries. An example is when adding contacts which have no entries. Basically I want to have one entry in the table which can be bound to the id within the table.

How can i do this?

My current code is

Insert into Contact(FirstName, Surname, Position, TelephoneNo, EmailAddress, RegisteredDate)

Select Distinct Import.FirstName, Import.SecondName, Import.JobTitle, 
                Import.ContactTelNumber, Import.EmailAddress, Import.RegistrationDate
FROM Import

This basically imports and does no checks but where can I add check for this?

It's hard to infer exactly what you mean from your description. It would help if you showed a couple of example lines, one that you want included and one that you want to be excluded.

But you can add a variety of conditions in the WHERE clause of your SELECT. For example, if you just want to make sure that at least one column in Import is non-null, you could do this:

INSERT INTO Contact(FirstName, Surname, Position, TelephoneNo, 
  EmailAddress, RegisteredDate)

SELECT DISTINCT FirstName, SecondName, JobTitle, 
                ContactTelNumber, EmailAddress, RegistrationDate
FROM Import
WHERE COALESCE(FirstName, SecondName, JobTitle, ContactTelNumber, 
  EmailAddress, RegistrationDate) IS NOT NULL

COALESCE() is a function that accepts a variable number of arguments, and returns the first non-null argument. If all the arguments are null, it returns null. So if we coalesce all the columns, and we get a null, then we know that all the columns are null, and we exclude that row.


Re your comment:

Okay, it sounds like you want a unique constraint over the whole row, and you want to copy only rows that don't violate the unique constraint.

One way to accomplish this would be the following:

ALTER TABLE Contact ADD UNIQUE KEY (FirstName, Surname, Position, TelephoneNo, 
  EmailAddress, RegisteredDate);

INSERT IGNORE INTO Contact(FirstName, Surname, Position, TelephoneNo, 
  EmailAddress, RegisteredDate)

SELECT DISTINCT FirstName, SecondName, JobTitle, 
                ContactTelNumber, EmailAddress, RegistrationDate
FROM Import;

The INSERT IGNORE means if it encounters an error like a duplicate row, don't insert it, but also don't abort the insert for other rows.

The unique constraint creates an index, so it will take some time to run that ALTER TABLE, depending on the size of your table.

Also it may be impractical to have a key containing many columns. Indexes have a limit of 16 columns and 1000 bytes total in length. However, I would expect that what you really want is to restrict to one row per EmailAddress or some other subset of the columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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