mysql - 试图完善查询

标签 mysql

<分区>

目前我只是在寻找矩形形式,所以我试图让它在 phpmyadmin 上使用 vanilla mysql,我想要一个数据库,其中有多个可能相互重叠的区域,每个区域都有一个每平方像素的“成本”,所以你可以得到在某一点的成本或测量整个区域的成本而忽略重叠的部分,你也可以得到数据库中存在的总成本的平均值。

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

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

最佳答案

首先纠正查询中的错误:

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 

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

关于mysql - 试图完善查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16070184/

相关文章:

php - 提高网站性能

php - 使用PHP回显Jquery执行.post();在 .click() 上;导致我无法解释的查询

mysql - 从复合主键约束中仅删除一个列约束

mysql - 我们如何找到 MySQL 中顺序编号的间隙?

mysql - 如何在 mysql 中使用关系时获取行数

mysql - 从表中选择两次 PHP SQL

PHP:设置不同的值并使用 foreeach 和 mysql 选择

mysql - 无法在 Debian 7 (Wheezy) 上安装 mySQL/maridDB

mysql - 具有相同类型实体但不同数据库的多个数据源

mysql - 如何删除 mySQL 以重新开始? (操作系统)