简体   繁体   English

MS Access上的SQL查询的溢出错误

[英]overflow error of sql query on MS Access

I need to find max value of a column for a table on MS Access by sql query. 我需要通过SQL查询在MS Access上查找表的列的最大值。

SELECT max( v_difference ) AS max_v_difference
FROM 
(
   SELECT *, vv1 - vv2 AS  v_difference , 
   FROM
   (
     SELECT table3.*  , table1.v1 AS vv1, table2.v1 AS vv2 
     FROM table1, table2, table3
     where table1.id = table2.id and table1.id <> "" and table3.id = table1.id
  )
 ) 

I got error: "overflow" 我收到错误消息:“溢出”

Any help would be appreciated. 任何帮助,将不胜感激。

thanks 谢谢

I'm guessing it has to do with the fact that you are performing implicit cross joins with your innermost subquery. 我猜想这与您对最里面的子查询执行隐式交叉联接有关。 While some SQL engines will optimize those types of queries automatically, MS Access is not one of them. 尽管某些SQL引擎会自动优化这些类型的查询,但MS Access并不是其中之一。

A cross join returns the Cartesian product of two tables; 交叉联接返回两个表的笛卡尔乘积; the Cartesian product is a combination of every row from one table combined with every row from another table. 笛卡尔积是一个表中的每一行与另一表中的每一行的组合。 So if table1 has 1,000 rows and table2 has 1,000 rows then the Cartesian product of those tables has 1,000 x 1,000 = 1,000,000 rows. 因此,如果table1具有1,000行,而table2具有1,000行,则这些表的笛卡尔积具有1,000 x 1,000 = 1,000,000行。

The situation gets worse quickly as you add tables. 当您添加表时,情况迅速恶化。 If your table3 has 10,000 rows, then the Cartesian product of all three tables is 1,000 x 1,000 x 10,0000 = 10,000,000,000 rows. 如果您的表3有10,000行,则所有三个表的笛卡尔积均为1,000 x 1,000 x 10,0000 = 10,000,000,000行。 You can see how combining even modestly sized tables could quickly overwhelm system resources and result in an overflow error. 您可以看到即使合并大小适中的表也可能很快使系统资源不堪重负,并导致溢出错误。

When you do an INNER JOIN, the resulting rowset is the intersection of the tables where the specified JOIN condition is met. 当您执行INNER JOIN时,结果行集是满足指定JOIN条件的表的交集。 This (almost*) always results in a smaller result set than a CROSS JOIN. 这(几乎*)总是导致结果集比CROSS JOIN小。

You should use INNER JOINs instead. 您应该改为使用INNER JOINs。 Try the following: 请尝试以下操作:

SELECT max( v_difference ) AS max_v_difference
FROM 
(
   SELECT vv1 - vv2 AS  v_difference
   FROM
   (
     SELECT t1.v1 AS vv1, t2.v1 AS vv2 
     FROM (table1 AS t1 INNER JOIN table2 AS t2 ON t1.id = t2.id)
           INNER JOIN table3 AS t3 ON t1.id = t3.id
     WHERE t1.ID <> ""
  )
 ) 

* It is possible to specify a join condition that will always evaluate to TRUE for every combination of rows. *可以指定一个连接条件,对于每个行组合,该条件始终为TRUE。 In such a case the result of the INNER JOIN would be the same as the CROSS JOIN. 在这种情况下,INNER JOIN的结果将与CROSS JOIN相同。 Of course, such a query would have no real-world value, other than to discredit explanations that use unconditionally absolute language, for example, "always" ;). 当然,这种查询将不具有现实价值,只不过是抹黑使用无条件绝对语言(例如“ always”;)的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM