1

I want to update a row based on the existence of an id in other table that is also in that table. In other words that id is a primary key in other table and just a column in the table I want to update.

I want to update based in the same id.

I have this query but it doesn't work because of the SQL syntax.

UPDATE
transaction
SET
    DaysRented = 3,
    Cost = 3,
    TotalCost= 5
FROM 
 transaction

INNER JOIN
    rentals
ON
     transaction.idRentals = rentals.idRentals;

2 Answers 2

2

syntax for mysql

http://www.mysqltutorial.org/mysql-update-join/

  1. First, you specify the main table ( T1) and the table that you want the main table to join to ( T2) after the UPDATE clause. Notice that you must specify at least one table after the UPDATE clause. The data in the table that is not specified after the UPDATE clause is not updated.
  2. Second, you specify a kind of join you want to use i.e., either INNER JOIN or LEFT JOIN and a join condition. Notice that the JOIN clause must appear right after the UPDATE clause.
  3. Third, you assign new values to the columns in T1 and/or T2 tables that you want to update.

UPDATE transaction 
INNER JOIN rentals  ON transaction.idRentals = rentals.idRentals
SET DaysRented = 3,
    Cost = 3,
    TotalCost = 5;
Sign up to request clarification or add additional context in comments.

Comments

2

You are using SQL Server update/join syntax. The proper MySQL syntax is:

UPDATE transaction INNER JOIN
       rentals
       ON transaction.idRentals = rentals.idRentals
SET DaysRented = 3,
    Cost = 3,
    TotalCost = 5;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.