简体   繁体   中英

SQL Syntax error on JOIN

MY TABLES:

        USERS_1:                             USERS_2:
   +------------+---------+        +------------+---------+
   |    id      |username |        |  username  |claimedBy| 
   +------------+---------+        +------------+---------+
   |     4      | pitiqu  |        | myUsername |  NULL   |<- this should become 4
   +------------+---------+        +------------+---------+

MY SQL: (Literally MySQL)

UPDATE UL
SET UL.claimedBy = US.username
FROM USERS_1 as UL
INNER JOIN USERS_2 as US
ON US.id = 4
where UL.username="myUsername"

It's probably obvious that i want to set table 2's claimed_by (for the username "myUsername") to the username "pitiqu" found in table 1 at id = 4.

I'm sorry if all the "username" is confusing. Hope the tables and the SQL clears my question.

The error that pops out:

#1064 - 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 USERS_1 as UL INNER JOIN USERS_2 as US ON US.id = 4 where UL' at line 3

Why is this happening... anyone?

EDIT : Excuse me for the incorrect syntax. I've been trying to use THIS example and while editing it I deleted the SET.

You could use a update query like this:

update
  USERS_2
set
  claimedBy = (SELECT username FROM USERS_1 WHERE id=4)
where
  username="myUsername"

if you want a join, the correct syntax is like this however on this particular context it doesn't make much sense and I would suggest you to use the first query:

UPDATE
  USERS_1 as UL INNER JOIN USERS_2 as US ON US.id = 4
SET
  UL.claimedBy = US.username
WHERE
  UL.username="myUsername"

That's a wrong syntax. You should use a update join like

UPDATE UL u
JOIN USERS_2 US ON US.id = 4
SET u.claimedBy = US.username
where u.username='myUsername';

You're using FROM in an UPDATE query. That is downright incorrect.

One way of rewriting it would be as below, making use of a subquery:

UPDATE USERS_2 set claimedBy = (SELECT id from USERS_1 where username = "pitiqu")
where username="myUsername";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM