How to fix error: "Subquery returned more than 1 value". Not permitted when subquery follows =, !=, <, <= , >, >= or used as an expression

How to fix error: "Subquery returned more than 1 value". Not permitted when subquery follows =, !=, <, <= , >, >= or used as an expression

完整的错误是:

Subquery returned more than 1 value. Not permitted when subquery follows =, !=, <, <= , >, >= or used as an expression

查询:

SqlConnection connect = Helper.GetCon;
string query = "select sum(amardate) as[All] ,"+
               "(select amardate from Amar where insertdate='" + 
                    DateTime.UtcNow.ToString("s") + "')as[Now]," +
               "(select amardate from Amar where insertdate='" +
                    DateTime.UtcNow.AddDays(-1).ToString("s") + "')as[Last] From Amar";

SqlDataAdapter da = new SqlDataAdapter(query, connect);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;

您的一个或多个子查询 return 编辑多行数据,在它只能 return 一个的上下文中。例如你遇到了这样的事情:

                  1234          1121
sum(amardate)  +  5678 AS now + 3141 AS last
                  9101          5161

其中 12341121 等...是由子查询编辑的额外行 return。现在数据库做什么?它不知道您要将这些多个值中的哪些加在一起。它也不能神奇地将这一行结果分成三行,因为它不知道应该如何进行拆分。

你需要确保两个子查询只能return ONE个结果行,由ONE个值组成。