2013-04-17 28 views
1

我只是寻找矩形形式的时刻,所以我试图让它与香草mysql在phpmyadmin上工作,我想有一个数据库,其中有多个区域可能有潜在的重叠,每个区域每平方像素都有一个“成本”,所以您可以在某个点获得成本或者测量整个区域的成本,同时忽略重叠的部分,也可以获得总成本的平均值在数据库中。试图完善查询

我想知道如果你们中的一个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 

这似乎是无效的,并充满了错误,也没有数据库架构被提及。我已经花了很多时间来尝试纠正它,并使其工作,但它一直返回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 
+2

你说“我*想*有一个数据库”,然后“没有提到数据库架构”。你有没有正在处理的数据库,或者你从哪里得到这个查询? – mellamokb

+1

检查内部查询中的where子句。PIXELX和PIXELX和DELTAX之间的'x.value可能应该是'PIXELX和PIXELX + DELTAX之间的x.value'。类似于'PIXELY' –

+0

你不能“测试一个查询”,更不用说“完美”,没有表来运行该查询,所以你不能说它是否“无效并且充满了bug”。我们应该在这里帮助你什么? –

回答

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 

然后,如果你仍然没有得到想要的结果,请单独尝试内部查询来查看返回的结果rom那。