UPDATE & Answer thanks and credit to @ypercube
THe Answer is below and I have taken the liberty of posting it as it appears in my PHP has a helper, I am using PDO
INSERT INTO `users_relationship` (`profile_1_id`, `profile_2_id` ,`request_date`,`auth_token`,`status`, `updated`, `deleted`)
VALUES
(:uid, :fid, '$now', '$fToken', 0, '$now',0),
(:fid, :uid, '$now', '$fToken', 0, '$now',0)
ON DUPLICATE KEY UPDATE request_date = VALUES(request_date), auth_token = VALUES(auth_token), updated = VALUES(updated), deleted = VALUES (deleted);
I have a MySQL query which is puzzling me, the requirement is that I need to create to rows in a table at one time however there maybe a possibility that these rows already exist and so I will need to update some of the columns in this row rather than insert duplicates.
Example
Justin (2) is related to Ben (5), the relationship is then terminated by updating the rows in the database where profile_1_id is 2 or 5 and inserting a flag into the deleted column. Ben then try’s to rekindle the relationship and therefore we need to update the rows again and remove the deleted flag and update request_date, auth_token & updated with new values.
I have written the following but it is not firing the error being:
Duplicate column name ‘2012-09-05 10:13:06’
INSERT INTO `users_relationship` (`profile_1_id`, `profile_2_id`,`request_date`,`auth_token`,`status`, `updated`, `deleted`)
SELECT * FROM
(SELECT 2, 5, '2012-09-05 10:13:06', 'SQLTEST', 0, '2012-09-05 10:13:06', 0) AS faTmp,
(SELECT 5, 2, '2012-09-05 10:13:06', 'SQLTEST', 0, '2012-09-05 10:13:06', 0) AS fTmp
WHERE NOT EXISTS
(SELECT * FROM `users_relationship`WHERE `profile_1_id`=2 AND `profile_2_id` =5) OR
(SELECT * FROM `users_relationship`WHERE `profile_1_id`=5 AND `profile_2_id` =2) LIMIT 1
Am I on the right path or is there a better way of doing this?
Thanks
Justin
The best way seems to be to add a
UNIQUEconstraint on(profile_1_id, profile_2_id)and then use: