MySQL无效查询:选择的嵌套级别太高

时间:2012-02-03 19:24:52

标签: mysql pivot-table

我将代码转换为安德鲁斯解决方案:

SELECT s1.biz_name, s1.biz_info, s1.e_address, s1.e_city, s1.e_state,
    s1.e_postal, s1.e_zip_full, s1.loc_LAT_centroid, s1.loc_LONG_centroid,
    s1.biz_phone, s1.biz_phone_ext, s1.biz_fax, s1.biz_email, s1.web_url,
    s2.upc as upc2, s2.retailprice as retailprice2, s2.dollar_sales as
    dollar_sales2, s2.dollar_sales_ly as dollar_sales_ly2, s2.todaydate as
    todaydate2, s2.datetimesql as datetimesql2, s2.shelfposition as
    shelfposition2, s2.reg_sale as reg_sale2, s2.representative as
    representative2, s2.notes as notes2, s3.upc as upc3, s3.retailprice as
    retailprice3, s3.dollar_sales as dollar_sales3, s3.dollar_sales_ly as
    dollar_sales_ly3, s3.todaydate as todaydate3, s3.datetimesql as
    datetimesql3, s3.shelfposition as shelfposition3, s3.reg_sale as reg_sale3,
    s3.representative as representative3, s3.notes as notes3, s4.upc as upc4,
    s4.retailprice as retailprice4, s4.dollar_sales as dollar_sales4,
    s4.dollar_sales_ly as dollar_sales_ly4, s4.todaydate as todaydate4,
    s4.datetimesql as datetimesql4, s4.shelfposition as shelfposition4,
    s4.reg_sale as reg_sale4, s4.representative as representative4, s4.notes as
    notes4, s5.upc as upc5, s5.retailprice as retailprice5, s5.dollar_sales as
    dollar_sales5, s5.dollar_sales_ly as dollar_sales_ly5, s5.todaydate as
    todaydate5, s5.datetimesql as datetimesql5, s5.shelfposition as
    shelfposition5, s5.reg_sale as reg_sale5, s5.representative as
    representative5, s5.notes as notes5 
FROM allStores AS s1 
LEFT OUTER JOIN storeCheckRecords AS s2
    ON s1.e_address = s2.e_address AND s2.upc = '650637119004' 
LEFT OUTER JOIN storeCheckRecords AS s3 
    ON s1.e_address = s3.e_address AND s3.upc = '650637119011' 
LEFT OUTER JOIN storeCheckRecords AS s4 
    ON s1.e_address = s4.e_address AND s4.upc = '650637374007' 
LEFT OUTER JOIN storeCheckRecords AS s5 
    ON s1.e_address = s5.e_address AND s5.upc = '650637374014' 
WHERE  s2.e_address IS NOT NULL
    OR s3.e_address IS NOT NULL
    OR s4.e_address IS NOT NULL
    OR s5.e_address IS NOT NULL

以下是新错误:查询无效:表太多; MySQL只能在连接中使用61个表

还有其他想法吗?谢谢你的帮助。

3 个答案:

答案 0 :(得分:1)

可能与此有关 MySQL bug #41156, List of derived tables acts like a chain of mutually-nested subqueries

错误日志表明它已针对MySQL 5.0.72,5.1.30和6.0.7进行了验证 修复了MySQL 5.1.37,MySQL 5.4.2(变为5.5.something)和NDB 7.1.0。


关于上述问题中重新设计的查询:

Pivot查询可能很棘手。您可以使用Andrew在his answer中建议的方法。如果搜索许多UPC值,则需要编写应用程序代码来构建SQL查询,并附加与您要搜索的UPC值数一样多的JOIN子句。

MySQL确实对单个查询中可以完成的联接数量有限制,但是您应该没有达到限制的示例。也就是说,您显示的查询确实有效。

我假设您正在显示搜索四个UPC代码的示例查询,而您的应用可能会为更多数量的UPC代码动态构建查询,有时可能超过61个。

您的查询的目标似乎是返回至少包含一个列出的UPC代码的商店。您可以在以下查询中更简单地执行此操作:

SELECT DISTINCT s.*
FROM allStores AS s
JOIN storeCheckRecords AS cr
  ON s.e_address = cr.e_address
     AND cr.upc IN ('650637119004','650637119011','650637374007','650637374014');

您可以通过其他方式使用此方法,例如查找包含所有四个UPC的商店:

SELECT s.*
FROM allStores AS s
JOIN storeCheckRecords AS cr
  ON s.e_address = cr.e_address
     AND cr.upc IN ('650637119004','650637119011','650637374007','650637374014');
GROUP BY s.e_address
HAVING COUNT(DISTINCT upc) = 4;

或者找到一些但不是全部四个UPC的商店:

SELECT s.*
FROM allStores AS s
JOIN storeCheckRecords AS cr
  ON s.e_address = cr.e_address
     AND cr.upc IN ('650637119004','650637119011','650637374007','650637374014');
GROUP BY s.e_address
HAVING COUNT(DISTINCT upc) < 4;

或者找到缺少所有四个UPC的商店:

SELECT s.*
FROM allStores AS s
JOIN storeCheckRecords AS cr
  ON s.e_address = cr.e_address
     AND cr.upc IN ('650637119004','650637119011','650637374007','650637374014');
WHERE cr.e_address IS NULL;

您仍然需要编写一些代码来构建此查询,但它更容易实现,并且它不会超出您可以运行的连接数或子查询数的任何限制。

答案 1 :(得分:0)

这可以在不使用子查询的情况下为您提供相同的结果:

SELECT s1.biz_name,
       ...
       s2.upc             AS upc2,
       ...
       s3.upc             AS upc3,
       ...
       s4.upc             AS upc4,
       ...
       s5.upc             AS upc5,
       ...
FROM allStores AS s1
LEFT OUTER JOIN storeCheckRecords AS s2 ON s1.e_address = s2.e_address
LEFT OUTER JOIN storeCheckRecords AS s3 ON s1.e_address = s3.e_address
LEFT OUTER JOIN storeCheckRecords AS s4 ON s1.e_address = s4.e_address
LEFT OUTER JOIN storeCheckRecords AS s5 ON s1.e_address = s5.e_address
WHERE  (s2.e_address IS NOT NULL
OR s3.e_address IS NOT NULL
OR s4.e_address IS NOT NULL
OR s5.e_address IS NOT NULL)  
AND (s2.upc = '650637119004' OR s2.upc IS NULL)
AND (s3.upc = '650637119011' OR s3.upc IS NULL)
AND (s4.upc = '650637374007' OR s4.upc IS NULL)
AND (s5.upc = '650637374014' OR s5.upc IS NULL)

答案 2 :(得分:0)

我会简化为首先使用简单的WHERE IN子句获取所有元素...您似乎正在执行数据透视表以显示T1到T2到T3到T4到T5。如果您获得单个行中的所有数据,那么您可以在顶部显示STATIC列,显示每行下方的详细信息。

SELECT 
      t1.brand,
      t1.biz_name,
      t1.biz_info,
      t1.e_address,
      t1.e_city,
      t1.e_state,
      t1.e_postal,
      t1.e_zip_full,
      t1.loc_LAT_centroid,
      t1.loc_LONG_centroid,
      t1.biz_phone,
      t1.biz_phone_ext,
      t1.biz_fax,
      t1.biz_email,
      t1.web_url,
      t1.upc,
      t1.retailprice,
      t1.dollar_sales,
      t1.dollar_sales_ly,
      t1.todaydate,
      t1.datetimesql,
      t1.shelfposition,
      t1.reg_sale,
      t1.representative,
      t1.notes 
   FROM 
      storeCheckRecords as t1 
   WHERE 
      t1.upc IN ( '650637119004', '650637119011', '650637374007', '650637374014') 

如..

Brand  Bus Addr                  UPC        Retail$     Sales        Notes
xyz    Bus Name                  UPC        ... etc...  Cur Yr 
       Bus Info                  Shelf Info             Last Yr
       Address, (Cit/State/Zip)
       Lat / Long
       Phone / Fax
       Email / Web

----
Next Entry

与携带物品的人相反,完全相同的地址是否真的很重要?如果一个条目是“123 Main St”,另一个是“123B Main St”和“123 Main St - Suite B”,你将找不到匹配。

此外,你提到一些有多达75个UPC代码...将它们放在一个单独的表中并将其用作第一个连接到“StoreCheckRecords”的表并将它们全部...而不是手动键入所有列后缀为2到75 ......或者下一次运行中只有17个,而另外4个......我认为你可能已经太固定了你想要从数据中得到的东西了。

你甚至可以通过常见的“e_address”进行GROUP,你最初想要基于的匹配,并将该组作为报告给用户的部分之间的中断提供。