Reputation: 407
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.
Upvotes: 0
Views: 380
Reputation: 1878
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";
Upvotes: 1
Reputation: 77866
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';
Upvotes: 2
Reputation: 49049
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"
Upvotes: 3