从mysql中检索不存在的值,或者附近的值

从mysql中检索不存在的值,或者附近的值,mysql,Mysql,我寻找的值不仅在给定的表中不存在,而且相关的值也不存在。有效值是介于-5和5之间的二维值,总共有121个值(给定其他约束) 因此,对于前者,我有以下几点: select x,y from ( select v.x, w.y from (select '-5' as x union all select '-4' union all select '-3' union all select '-2' union all select '-1' union all sel

我寻找的值不仅在给定的表中不存在,而且相关的值也不存在。有效值是介于-5和5之间的二维值,总共有121个值(给定其他约束)

因此,对于前者,我有以下几点:

select x,y from (
  select
   v.x,
   w.y
  from 
   (select '-5' as x union all select '-4' union all select '-3' union all select '-2' union all select '-1' union all select '0' union all select '1' union all select '2' union all select '3' union all select '4' union all select '5') as v
  join
   (select '-5' as y union all select '-4' union all select '-3' union all select '-2' union all select '-1' union all select '0' union all select '1' union all select '2' union all select '3' union all select '4' union all select '5') as w
  left join
   (select x,y,id from building where body_id = ?) as b on v.x = b.x and w.y = b.y
  where
   b.id is null
) as t
这给了我一个没有“建筑物”的x,y坐标列表。然而,我想知道的是,是否有x,y,其中不仅是空的,而且它周围的(x-1到x+1,y-1到y+1)也是空的。根据定义,+/-5的任何x和+/-5的任何y不一定会使周围的所有x都为空


我想,最糟糕的情况是,拉取上述信息,并在每个值上搜索自己,看看是否所有周围的值都找不到。我要求更多的人用它作为学习更多SQL的借口。

您查询的关键概念是这一行:关于v.x=b.x和w.y=b.y。 (从这个小片段到结尾:

(select x,y,id from building where body_id = ?) as b on v.x = b.x and w.y = b.y where b.id is null
这意味着您只对v.x和w.y正好位于建筑坐标上的连接感兴趣

更改该条件很简单。例如,尝试以下操作:

select x,y from 
(
    select v.x, w.y from 
    (
        select '-5' as x union all select '-4' union all select '-3' union all select '-2' union all select '-1' union all select '0' union all select '1' union all select '2' union all select '3' union all select '4' union all select '5'
    ) as v
    join
    (
        select '-5' as y union all select '-4' union all select '-3' union all select '-2' union all select '-1' union all select '0' union all select '1' union all select '2' union all select '3' union all select '4' union all select '5'
    ) as w
    left join    
    (
        select x,y,id from building where body_id = ?
    ) as b 
    on 
    (
        (v.x + 1 = b.x and w.y + 1= b.y) OR
        (v.x + 1 = b.x and w.y = b.y) OR
        (v.x + 1 = b.x and w.y - 1= b.y) OR
        (v.x = b.x and w.y + 1= b.y) OR
        (v.x = b.x and w.y = b.y) OR
        (v.x = b.x and w.y - 1= b.y) OR
        (v.x - 1 = b.x and w.y + 1= b.y) OR
        (v.x - 1 = b.x and w.y = b.y) OR
        (v.x - 1 = b.x and w.y - 1= b.y) 
     )
     where b.id is null
) as t
这不会像您定义的那样处理边界情况(+/-5,在x或y上),至少,不是以我理解您的表的方式,但是您可以向“on”条件添加额外的or语句

另外,我手头没有一个表或数据库连接,所以这没有经过测试。可能会有一些小错误,但它们应该很容易修复

编辑: 我刚意识到我可能混淆了你想要的空间。这有点混乱,因为我们看不到你正在使用的表格

经过一点思考,我认为您最有可能想要一个条件,在ON子句中执行类似的操作(我现在将+/-替换为发生在建筑位置上,而不是坐标):

(b.x + 1 = v.x and b.y + 1= w.y) OR
(b.x + 1 = v.x and b.y = w.y) OR
(b.x + 1 = v.x and b.y - 1= w.y) OR
(b.x = v.x     and b.y + 1= w.y) OR
(b.x = v.x     and b.y = w.y) OR
(b.x = v.x     and b.y - 1= w.y) OR
(b.x - 1 = v.x and b.y + 1= w.y) OR
(b.x - 1 = v.x and b.y = w.y) OR
(b.x - 1 = v.x and b.y - 1= w.y)