Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8495527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:35:53+00:00 2026-06-10T23:35:53+00:00

UPDATE & Answer thanks and credit to @ypercube THe Answer is below and I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:35:55+00:00Added an answer on June 10, 2026 at 11:35 pm

    The best way seems to be to add a UNIQUE constraint on (profile_1_id, profile_2_id) and then use:

    INSERT INTO `users_relationship` 
      (`profile_1_id`, `profile_2_id`
      ,`request_date`,`auth_token`,`status`, `updated`, `deleted`)
    VALUES
      (2, 5, '2012-09-05 10:13:06', 'SQLTEST', 0, '2012-09-05 10:13:06', 0),
      (5, 2, '2012-09-05 10:13:06', 'SQLTEST', 0, '2012-09-05 10:13:06', 0) 
    ON DUPLICATE KEY UPDATE
      request_date = VALUES(request_date)          --- what you want to happen
    , auth_token = VALUES(auth_token)              --- when rows exist
      --- ; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 buttons in my html page, say ADD , UPDATE & DELETE
i have payment table fields update reason and amount & total field are change
UPDATE - Cause found! ... please read below & suggest the solution: While creating
UPDATE: SOLVED!!! Please see the answer I added below Does anyone know if there
How does INSERT, UPDATE & DELETE work on a SQL Server table partition? Technical
I'm working on an iOS App (FW: 5.0+ & ARC) which needs to update
Updated-2 I have interesting combination of warnings & errors. Firstly, when debugging, i get
update: I mistyped 2 variables...so embarrassing. thanks everyone for the effort! sorry i find
Question Answer below Hello I'm looking a simple way to change the colour of
Update (21st Sept 2016) - Thanks to Digbyswift for commenting that this solution still

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.