1

How can I use a result of select in update query? For example:

 Update access_rights 
 set rfidcode=(Select rfidcode from users where name like 'thomas')
 where id_access_rights=3;

This doesn't work. Can anyone help me ?

1
  • 1
    This question is asked fairly often. 3rd time today? Commented Aug 10, 2015 at 16:47

2 Answers 2

1

assuming you want to update a single record your select query needs to return a single result. use the limit keyword.

Update access_rights 
set rfidcode=(Select rfidcode from users where name like 'thomas' limit 1)
where id_access_rights=3;

vkp solution if you want to update many related records but you have to watch your joins or you'll get errors, or worse corrupt your data

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I did it like this :) Thank you too
1
Update access_rights a, users u
set a.rfidcode = u.rfidcode  
where a.userid = u.userid --change this to appropriate join column
and u.name like '%thomas%'
and a.id_access_rights=3;

1 Comment

thanks but I did it myself like this update access_rights set users_rfidcode= (select rfidcode from users where nickname like 'kin') where id_access_rights=14;

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.