2013-04-17 58 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那。