在mysql中使用SELECT插入值

在mysql中使用SELECT插入值,mysql,sql,sql-insert,Mysql,Sql,Sql Insert,我有两个表:列id为、用户名为、密码为的用户,列user_id为、failed为、时间为的user_failed。是否有任何可能的方法可以将仅使用用户名失败的用户_插入到表中?我尝试了此代码,但失败: INSERT INTO `login_attempts`(`user_id`, `time`, `failed`) VALUES (SELECT user_id FROM users WHERE username = 'pokemon','',3) 您的SQL查询不正确有几个原因 如果我正确地

我有两个表:列id为、用户名为、密码为的用户,列user_id为、failed为、时间为的user_failed。是否有任何可能的方法可以将仅使用用户名失败的用户_插入到表中?我尝试了此代码,但失败:

INSERT INTO `login_attempts`(`user_id`, `time`, `failed`) 
VALUES (SELECT user_id FROM users WHERE username = 'pokemon','',3)

您的SQL查询不正确有几个原因

如果我正确地解释了您的查询,以下内容应该可以使用

INSERT INTO `login_attempts`(`user_id`, `time`, `failed`)
SELECT id, '', 3 FROM users WHERE username = 'pokemon'
从SELECT插入表格不需要值。以下是如何使用值的示例…:


另外,您的子查询从username='pokemon',3中选择user_id,WHERE子句无效。具体来说,我假设的第3部分是您希望插入的时间值,但失败了。

这将起作用……您必须在语句前后添加普通括号


插入'login\u truments``user\u id`、'time`、'failed`值从username='pokemon'的用户中选择user\u id,'3it's i can't i get this error 1054-未知列'field list'中的'user\u id'啊,这是工作,我只是将user\u id更改为id,因为在我的users表中它是id而不是user\u id。谢谢括号?你的意思是普通的括号。
INSERT INTO `login_attempts`(`user_id`, `time`, `failed`)
VALUES (1, '', 3)