Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Inner Join Update

I am trying to perform a relational update using an innjer join and can't seem to get the syntax correct.

The rows are as follows:

Catalogue.Category_ID
Catalogue.Visible

Categories.Category_ID
Categories.Category_Name

I am trying to update the value of the visible field when the category ID numbers match and correspond the the correct name.

UPDATE `Catalogue` 
SET `Visible` = '0' 
FROM `Catalogue` 
INNER JOIN `Categories` 
  ON Catalogue.Category_ID = Categories.Category_ID 
  AND Categories.Category_Name = 'Bases'

Apologies if it comes down to a stupid syntax mistake, i'm not the most experienced with relational databases.

like image 263
Wesk Avatar asked Jul 26 '13 20:07

Wesk


People also ask

Can we use inner join in update statement MySQL?

The MySQL Update Join is used for executing the update statement together with the implementation of INNER JOIN and LEFT JOIN MySQL clauses in the server. This Update JOIN clause in MySQL helps to retrieve the data records from the related database tables along with modifying them with the query.

Can we join two tables in update query?

It is possible to join two or more tables in an UPDATE query.


2 Answers

I believe this is the proper syntax:

UPDATE `Catalogue` 
INNER JOIN `Categories` 
  ON Catalogue.Category_ID = Categories.Category_ID 
  AND Categories.Category_Name = 'Bases'
SET `Visible` = '0' 
like image 70
Hart CO Avatar answered Sep 29 '22 06:09

Hart CO


You are using TSQL syntax, here's for MySQL

UPDATE `Catalogue` 
       INNER JOIN `Categories`
           ON Catalogue.Category_ID = Categories.Category_ID

SET `Visible` = '0' 
WHERE Categories.Category_Name = 'Bases'

if you want to shorten the query, use ALIAS

UPDATE Catalogue a 
       INNER JOIN Categories b
           ON a.Category_ID = b.Category_ID

SET    Visible = '0' 
WHERE  b.Category_Name = 'Bases'
like image 39
John Woo Avatar answered Sep 29 '22 06:09

John Woo