试图完善查询

时间:2013-04-17 21:01:22

标签: mysql

我只是在寻找矩形表格,因此我想在phpmyadmin上使用vanilla mysql,我希望有一个数据库,其中有多个区域可能会相互重叠,每个区域都有一个每平方像素的“成本”,因此您可以在某个点获得成本或测量整个区域的成本,同时忽略重叠的部分,您还可以获得数据库中现有总成本的平均值。

我想知道你们中的一位mysql兽医是否可以帮助我编写这样的查询/数据库架构,我已经有了或多或少的东西

select sum(c.cost) 
from (select x.num as x, y.num as y, max(priority) as maxpriority 
      from numbers x 
      cross join numbers y 
      join costs c 
      on x.num between c.x and c.x + c.deltax 
      and y.num between c.y + c.deltay 
      where x.value between PIXELX and PIXELX and DELTAX and 
      y.value between PIXELY and PIXELY and DELTAY 
      group by x.num, y.num) xyp
join costs c 
on xyp.x between c.x and c.x + c.deltax
and xyp.y between c.y + c.deltay
and xyp.maxpriority = c.priority 

似乎无效且充满了bug,也没有提到数据库架构。我已经花了很多天时间来尝试纠正它并使其工作但它一直返回null。

提前致谢,我很感激。

以下是架构: 这是数字:

Collation   Attributes  Null    Default Extra   Action
     1  num int(11)         No  None          Change      Drop    Browse distinct values     More

这是费用:

#   Name    Type    Collation   Attributes  Null    Default Extra   Action
 1  x   int(11)         No  None          Change      Drop    Browse distinct values     More
 2  y   int(11)         No  None          Change      Drop    Browse distinct values     More
 3  deltax  int(11)         No  None          Change      Drop    Browse distinct values     More
 4  deltay  int(11)         No  None          Change      Drop    Browse distinct values     More
 5  priority    int(11)         No  None          Change      Drop    Browse distinct values     More

1 个答案:

答案 0 :(得分:0)

首先纠正查询中的错误:

select sum(c.cost) 
from (select x.num as x, y.num as y, max(priority) as maxpriority 
      from numbers x 
      cross join numbers y 
      join costs c 
      on x.num between c.x and c.x + c.deltax 
      and y.num between c.y + c.deltay 
      where (x.value between PIXELX and PIXELX + DELTAX)   -- + instead of and
      and (y.value between PIXELY and PIXELY + DELTAY)     -- + instead of and
      group by x.num, y.num) xyp
join costs c 
on (xyp.x between c.x and c.x + c.deltax)
and (xyp.y between c.y and c.y + c.deltay)                 -- added c.y
and xyp.maxpriority = c.priority 

然后,如果仍然没有得到所需的结果,请单独尝试内部查询以查看从中返回的内容。