0

I'm trying to make an UPDATE statement with an INNER JOIN in (MY)SQL. I already took examples and edited the table names and table columns to mine. But my MYSQL still keep giving me an error (#1064).

ERROR

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM favorites INNER JOIN images ON favorites.image_id = images.id W' at line 5

SQL

UPDATE
  favorites
SET
  favorites.archive = 1
FROM
  favorites
INNER JOIN
  images
ON
  favorites.image_id = images.id
WHERE
  favorites.user_id = '1'

Thanks in advance in helping me out on this one.

Kind regards,

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
Glenn S
  • 21
  • 7
  • Please see the other question, or check [the docs](https://dev.mysql.com/doc/refman/8.0/en/update.html) – GolezTrol Oct 23 '18 at 08:08

2 Answers2

2

Update syntax is different in MySQL. You cannot use From clause inside an Update statement.

It is like:

Update (all the tables with join relationships if any) 
Set (field = value/expression) 
Where (all the conditions)

Try the following query instead:

UPDATE
  favorites 
INNER JOIN
  images
ON
  favorites.image_id = images.id
SET
  favorites.archive = 1
WHERE
  favorites.user_id = '1'

Full syntax is:

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET assignment_list
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

value:
    {expr | DEFAULT}

assignment:
    col_name = value

assignment_list:
    assignment [, assignment] ...
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
1

The following should to the trick for you.

UPDATE favorites 
INNER JOIN images ON favorites.image_id= images.id
SET favorites.archive = 1 
WHERE favorites.user_id = '1';
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156