Update column with variable



  • Employee table

    ID EmployeeSRN Name SupervisorID
    1 10101 Akash 2
    2 10111 Smeeta 4
    3 10201 samir 5

    I want to update SupervisorID of Akash to Samir's ID, i.e. 3. In my C# function, the only parameter I receive is Akash's EmployeeSRN and Samir's EmployeeSRN.

    How can I write a single update statement to update Akash's SupervisorID to 3, in mysql?



  • UPDATE Employee
        SET SupervisorID =
           ( SELECT ID
                 FROM Employee
                 WHERE EmployeeSRN = ?
           )
        WHERE EmployeeSRN = ? ;
    

    (Someone else can advise on how to substitute values into the query for C# without risking sql-injection.)




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2