0

I have two tables:

users (id, email)
devices (id,name, userid)

I want to insert a new device on insert ther userid based on the email in the users table:

INSERT INTO devices (name, userid) VALUES ('devicename', retrievedUserId)

How can I pass the id from the users table to above query?

SELECT id from users WHERE email = '[email protected]'

I would like something like this:

INSERT INTO devices (name, userid) VALUES ('devicename', SELECT id from users WHERE email = '[email protected]')

But with correct syntax :) It has been a while since I wrote vanilla SQL

1 Answer 1

2
INSERT INTO devices (name, userid)  
SELECT 'devicename',id from users WHERE email = '[email protected]'
Sign up to request clarification or add additional context in comments.

Comments

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.