Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何将子查询作为值之一插入多行?_Sql_Postgresql_Sql Insert - Fatal编程技术网

Sql 如何将子查询作为值之一插入多行?

Sql 如何将子查询作为值之一插入多行?,sql,postgresql,sql-insert,Sql,Postgresql,Sql Insert,我试图在一个表中插入多行,其中一列的值来自另一个查询。然而,我得到了以下错误 由用作表达式的子查询返回的多行 我该怎么做 INSERT INTO accounts_account_preferences (account_id, preference_id) VALUES ((SELECT account_id FROM accounts_account_preferences WHERE preference_id = 1), 2); 您的内部查询应

我试图在一个表中插入多行,其中一列的值来自另一个查询。然而,我得到了以下错误

由用作表达式的子查询返回的多行

我该怎么做

INSERT INTO 
   accounts_account_preferences (account_id, preference_id) 
VALUES 
   ((SELECT account_id 
     FROM accounts_account_preferences 
     WHERE preference_id = 1), 2);

您的内部查询应该每个外部行返回1条记录。如果它返回多条记录,则它将不起作用。

使用插入。。。选择没有价值观:


运行“从帐户\帐户\首选项中选择帐户\ id,其中首选项\ id=1”查询时,您会得到什么输出。我会得到一个帐户\ id列表,这就是您出错的原因。查询应该返回一个精确的结果。是否要插入所有帐户id,并将所有帐户的preferecen_id设置为2?这意味着您想插入多行?您应该显示示例数据。.是的,我想插入所有返回的帐户\u id,首选项为\u id=2欢迎使用堆栈溢出!解释尝试失败的原因是可以的,但您还需要提供原始问题的实际解决方案
INSERT INTO accounts_account_preferences (account_id, preference_id) 
SELECT account_id, 2 
FROM accounts_account_preferences 
WHERE preference_id = 1