Duke
Duke

Reputation: 36970

Skip insert on duplicate entry in mysql

I have a favorite table containing some fields like login_id,driver_id(One login_id may have many driver_id) . Then I am using bulk update , not having any check the driver_id is exists in table .

Input data will be set of driver ids , I need to skip the insertion of driver id if the same id already be there for relevant login_id . So the new ids will be inserted and others are not

What can I do with mysql , what are all the settings needed in table .

Please advise me

Upvotes: 7

Views: 12052

Answers (2)

jeewiya
jeewiya

Reputation: 571

You can use "INSERT IGNORE INTO".it's return updated row count.

INSERT IGNORE INTO favorite(login_id,driver_id) VALUES (login_id,driver_id) (login_id,driver_id).....

Upvotes: 14

Ben Rowe
Ben Rowe

Reputation: 28711

create a unique key with login_id and driver_id in mysql.

When you try to insert an existing record based on those keys, an error will be thrown. You can then trap this error and proceed as normal.

CREATE UNIQUE INDEX unique
  ON tbl_name (login_id, driver_id)

Upvotes: 4

Related Questions