My Table structure
table: marks
My objective: i want to insert or update multiple records with the condition
i am currently check by this query
1st step
SELECT * FROM `marks` WHERE `student` =115 AND `param` =1
2nd step
if
records found by matching above criteria i just update record by my new values
else
insert new record into my table
It gonna working fine . but i want to reduce code and optimize this into single query . its possible or not ? I found this on MySQL docs INSERT ... ON DUPLICATE KEY UPDATE . if this is a solution . how can i achieve by query ?
Note: i am using the Yii framework . suggestion from Yii also welcome
Edited: This query does't not update the rows . but escape from insert working correctly
INSERT INTO marks(`student`,`param,mark`,`created`,`lastmodified`,`status`)
VALUES
(11,30,10,'00-00-00 00:00:00','00-00-00 00:00:00','U')
ON DUPLICATE KEY UPDATE `mark`=VALUES(`mark`)
ON DUPLICATE KEY UPDATE SET mark=VALUES(mark)
– WrikkenALTER TABLE marks ADD UNIQUE (student, param);
, and thenINSERT INTO marks (student, param, mark, created) VALUES (11,20,10,NOW()) ON DUPLICATE KEY UPDATE SET mark=VALUES(mark);
should work. – Wrikken