ToddN
ToddN

Reputation: 2961

MySQL INSERT only if two fields don't match

I have 6 fields with the primary as ID and is set to auto_increment. I want to INSERT a new row if DATE and FROM do not match. I was thinking of REPLACE INTO or ON DUPLICATE KEY UPDATE but from what I know I have to have one of them as my Primary? I don't care how its done I just need some help with a query that would work.

ID

DATE        

STORE               

TOTAL           

NPS     

FROM

Upvotes: 2

Views: 2507

Answers (1)

Yanick Girouard
Yanick Girouard

Reputation: 4960

What you need is a unique index composed of both the DATE and FROM fields.

ALTER TABLE table ADD UNIQUE INDEX(DATE, FROM);

Then you can use this type of query:

INSERT IGNORE INTO table (columns) VALUES (...)

The IGNORE statement will skip any INSERT that would otherwise cause a duplicate key error.

Upvotes: 6

Related Questions