Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing MySQL IGNORE on one column under certain conditions
    primarykey
    data
    text
    <p>I want to import records from Gmail into a table, and I do not need duplicates for each account.</p> <p><strong>Description:</strong></p> <p>I have a table named list with definition below:</p> <pre><code>id int(11), account_id int(11), email varchar(255), phone varchar(30), primary key(id), FOREIGN KEY (account_id) REFERENCES accounts (id) </code></pre> <p>This table holds records for different accounts and an email can be considered valid for two or more accounts. This means that an email can repeat in a table but can only appear once for each account_id.</p> <p>I imported my contacts from Gmail (which is above 700 contacts and other users may have more than that).</p> <p><strong>The challenge:</strong></p> <p>I have an option of running two queries (one to check if email or phone exists, the second to insert record) for each record which in my case is 1,400 SQL queries to enable me insert all imported records, ensuring there are no duplicates for each account_id in the list table.</p> <p>I have looked at MySQL IGNORE and similar keywords like ON DUPLICATE KEY UPDATE but they do not seem to work in this scenario as I cannot make the email and phone columns unique as they can contain duplicate content.</p> <p>What is the best way of inserting these 700 records ensuring that the email and phone are not repeated for each account_id without having to run 1,400 queries?</p> <p><strong>QUESTION UPDATE:</strong></p> <p>I do not think INSERT IGNORE CAN WORK HERE FOR THE FOLLOWING REASONS:</p> <ol> <li>I cannot make email and phone unique columns</li> <li>The phone number may be empty but with an email entry, this may break the unique pattern</li> </ol> <p><strong>QUESTION ILLUSTRATION</strong></p> <p>I have two offices using the table to store their customer records. Someone can be a customer to both offices. This means his record can appear twice in the table but can only appear once for each account_id in the table. The challenge now is to insert several records into the table ensuring that a record does not repeat for each account_id.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload