我能够在MS Access中运行以下代码,该代码能够在“NodeForce”表中添加新行。
INSERT INTO [NodeForce] (nodeID, mem1, mem1min)
SELECT '1113', '1752-1', [Row_ID]
FROM [Element Forces - Frames] WHERE [FrameElem] = '1752-1'
AND [OutputCase] = 'Case2' AND [ElemStation] = 0
但是,当我添加一个额外的子查询时,例如:
INSERT INTO [NodeForce] (nodeID, mem1, mem1min, mem1max)
SELECT '1113', '1752-1', [Row_ID]
FROM [Element Forces - Frames] WHERE [FrameElem] = '1752-1'
AND [OutputCase] = 'Case2' AND [ElemStation] = 0,
[Row_ID] FROM [Element Forces - Frames] WHERE [FrameElem] = '1752-1'
AND [OutputCase] = 'Case6' AND [ElemStation] = 12
它会产生错误“查询值和目标字段的数量不同”。
如何使用不同的子查询插入多个值?
我也尝试过:
INSERT INTO ...
VALUES ('1113', '1752-1', (SELECT ... FROM ...), (SELECT ... FROM ...))
然后给了我一个错误,说“查询输入必须包含至少一个表或查询”
提前感谢您的帮助!
答案 0 :(得分:1)
解析您的查询:
INSERT INTO [NodeForce] (nodeID, mem1, mem1min, mem1max)
SELECT '1113', '1752-1', [Row_ID]
FROM [Element Forces - Frames] WHERE [FrameElem] = '1752-1'
AND [OutputCase] = 'Case2' AND [ElemStation] = 0
您正在向4列插入3个值,因此消息为"Number of query values and destination fields are not the same."
下面的那个没有SELECT
,并且在SELECT
语句之后有三个值
而不是在第一个[Row_ID]
语句的SELECT
之后。
,[Row_ID] FROM [Element Forces - Frames] WHERE [FrameElem] = '1752-1'
AND [OutputCase] = 'Case6' AND [ElemStation] = 12
因此,您需要的是将列数与您的值相匹配
您需要将第二个SELECT
作为第一个查询的一部分的子查询
并且只会返回1个值,例如:
INSERT INTO [NodeForce] (nodeID, mem1, mem1min, mem1max)
SELECT '1113', '1752-1', [Row_ID], (SELECT
[Row_ID] FROM [Element Forces - Frames] WHERE [FrameElem] = '1752-1'
AND [OutputCase] = 'Case6' AND [ElemStation] = 12 LIMIT 1)
FROM [Element Forces - Frames] WHERE [FrameElem] = '1752-1'
AND [OutputCase] = 'Case2' AND [ElemStation] = 0
其中:
(SELECT
[Row_ID] FROM [Element Forces - Frames] WHERE [FrameElem] = '1752-1'
AND [OutputCase] = 'Case6' AND [ElemStation] = 12 LIMIT 1)
用于mem1max
,这是一个只返回1个值的子查询
是第4个匹配您的列数4的值。