我有两张桌子:
users (id, email)
devices (id,name, userid)
我想根据users表中的电子邮件在insert ther userid上插入一个新设备:
INSERT INTO devices (name, userid) VALUES ('devicename', retrievedUserId)
如何将users表中的id传递给上面的查询?
SELECT id from users WHERE email = 'xxx@xxx.com'
我想要这样的事情:
INSERT INTO devices (name, userid) VALUES ('devicename', SELECT id from users WHERE email = 'xxx@xxx.com')
但语法正确:)自从我写了vanilla SQL
以来已经有一段时间了答案 0 :(得分:2)
INSERT INTO devices (name, userid)
SELECT 'devicename',id from users WHERE email = 'xxx@xxx.com'