gpt4 book ai didi

sql - 查询太复杂?使用追加查询

转载 作者:搜寻专家 更新时间:2023-10-30 22:34:26 27 4
gpt4 key购买 nike

我已经使用这个追加查询好几天了,它随机抛出这个异常:

Query is too Complex

查询将从联合查询中检索回的数据插入到不同的表中。

INSERT INTO [CA-SSI Dispatch]
SELECT
SSIQuery.[Phonenumber] AS Phonenumber,
SSIQuery.[FormRecordNumber] AS FormRecordNumber,
SSIQuery.[InspectedBY] AS InspectedBY,
SSIQuery.[Project] AS Project,
SSIQuery.[DateTimeInspection] AS DateTimeInspection,
SSIQuery.[Question] AS Question,
SSIQuery.[Answer] AS Answer,
SSIQuery.[Rank] AS Rank,
SSIQuery.[Comment] AS Comment,
SSIQuery.[Regulation] AS Regulation
FROM SSIQuery

WHERE NOT EXISTS
(SELECT * FROM [CA-SSI Dispatch] WHERE SSIQuery.[Phonenumber] = [CA-SSI Dispatch].[Phonenumber]
AND SSIQuery.[FormRecordNumber] =[CA-SSI Dispatch].[FormRecordNumber]
AND SSIQuery.[Project] = [CA-SSI Dispatch].[Project]
AND SSIQuery.[DateTimeInspection] = [CA-SSI Dispatch].[DateTimeInspection]
AND SSIQuery.[Question] = [CA-SSI Dispatch].[Question]
AND SSIQuery.[Answer] = [CA-SSI Dispatch].[Answer]
AND SSIQuery.[FormRecordNumber] = [CA-SSI Dispatch].[FormRecordNumber]);

在我向联合查询添加更多字段后,INSERT INTO 停止工作并不断返回该错误。如果有帮助,这是我的联合查询的一部分。这个 UNION QUERY,继续工作。

SELECT
SSIAns.phonenumber AS Phonenumber,
SSIAns.Form_Record_Number As FormRecordNumber,
SSIAns.Inspected_By AS InspectedBY,
SSIAns.Project,
SSIAns.Date_Time_of_Inspection AS DateTimeInspection,
QR.q1 AS Question,
SSIAns.Clean_level_ground AS Answer,
SSIRank.Rank_1 AS Rank,
SSIComment.Comment_1 AS Comment,
QR.r1 AS Regulation

FROM SSIAns, SSIRank,SSIComment, QR

WHERE
SSIRank.Rank_1 = "2_Address_Within_3_Days" AND
SSIAns.Form_Record_Number=SSIRank.Form_Record_Number
AND SSIRank.Form_Record_Number =SSIComment.Form_Record_Number

UNION
SELECT
SSIAns.phonenumber AS Phonenumber,
SSIAns.Form_Record_Number As FormRecordNumber,
SSIAns.Inspected_By AS InspectedBY,
SSIAns.Project,
SSIAns.Date_Time_of_Inspection AS DateTimeInspection,
QR.q2 AS Question,
SSIAns.Adequate_ramps_barricading AS Answer,
SSIRank.Rank_2 AS Rank,
SSIComment.Comment_2 AS Comment,
QR.r1 AS Regulation

FROM SSIAns, SSIRank,SSIComment, QR

WHERE
SSIRank.Rank_2 = "2_Address_Within_3_Days" AND
SSIAns.Form_Record_Number=SSIRank.Form_Record_Number
AND SSIRank.Form_Record_Number =SSIComment.Form_Record_Number

联合查询对下一个相应的字段进行一遍又一遍。例如:rank_3、rank_4 等

两个查询都运行良好,直到我加起来直到第 50 个

UNION 
SELECT
SSIAns.phonenumber AS Phonenumber,
SSIAns.Form_Record_Number As FormRecordNumber,
SSIAns.Inspected_By AS InspectedBY,
SSIAns.Project,
SSIAns.Date_Time_of_Inspection AS DateTimeInspection,
QR.q2 AS Question,
SSIAns.Adequate_ramps_barricading AS Answer,
SSIRank.Rank_50 AS Rank,
SSIComment.Comment_50 AS Comment,
QR.r1 AS Regulation

FROM SSIAns, SSIRank,SSIComment, QR

WHERE
SSIRank.Rank_50 = "2_Address_Within_3_Days" AND
SSIAns.Form_Record_Number=SSIRank.Form_Record_Number
AND SSIRank.Form_Record_Number =SSIComment.Form_Record_Number

现在,只有联合查询有效,当我尝试运行 INSERT INTO 查询以插入辅助表时,我运行错误“查询太复杂”通过联合查询,我们试图将问题分解为相应的答案、排名和评论,并合并到一条记录中。所以问题 1 & 相应的字段是一条记录。问题 2 & 相应的字段是不同的记录。

最佳答案

看起来您将表 QR 添加到您的查询中,但没有将其加入任何可能导致错误的内容。

在 were 子句中,您需要使 QR 表字段等于另一个表,以便将其连接在一起并拉入查询。

这一切都是次要的,首先也是最重要的是将您的代码标准化为使用连接而不是“FROM table1,table2,table3)”的方法

相反,您想这样做: 从表 1 内部连接表 2 ON table1.column1 = TABLE2.column1

等...

如果您提供完整的模式以及表的外键和主键,我可以给您一个准确的答案。

关于sql - 查询太复杂?使用追加查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39044151/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com