如何查询连接的坐标?

时间:2016-07-17 19:28:09

标签: mysql sql

我有一个表格,我根据X和Y坐标(XY是主键)映射值

+---+---+--------+--------+
| X | Y | Data 1 | Data 2 |
+---+---+--------+--------+

假设我存储了那些值:

    X →
    1234567
Y 1|XXXX  X|
↓ 2| XX   X|
  3|    X X|
  4| X   XX|
  5| X XXXX|

如何构建SQL查询以获取所有连接的值?获取顺序并不重要。

例如,如果我查询:

  • X=3Y=2提取:(1,1),(2,1),(3,1),(4,1),(2,2),(3,2)
  • X=5Y=3提取:(5,3)
  • X=2Y=4提取:(2,4),(2,5)
  • X=7Y=3提取:(4,5),(5,5),(6,4),(6,5),(7,1),(7,2),(7,3),(7,4),(7,5)

示例表为SQL:

DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `x` int(11) NOT NULL,
  `y` int(11) NOT NULL,
  PRIMARY KEY (`x`,`y`)
);
INSERT INTO `test` VALUES
(1, 0),(1, 1),(2, 2),(2, 4),(2, 5),(3, 1),(3, 2),(4, 1),(4, 5),
(5, 0),(5, 5),(6, 4),(6, 5),(7, 1),(7, 2),(7, 3),(7, 4),(7, 5);

3 个答案:

答案 0 :(得分:2)

您提出的问题可以通过公用表格来实现。

我在这个例子中使用TSQL,但是可以使用其他SQL方言构建类似的查询。

让我们先看一个列出所有相邻单元格的视图:

create view [dbo].[fromto] as
select
t1.x as x_from, 
t1.y as y_from,
t2.x as x_to,
t2.y as y_to
from test t1
join test t2 on t2.x = t1.x and t2.y = t1.y + 1
union
select
t1.x as x_from, 
t1.y as y_from,
t2.x as x_to,
t2.y as y_to
from test t1
join test t2 on t2.x = t1.x and t2.y = t1.y - 1
union
select
t1.x as x_from, 
t1.y as y_from,
t2.x as x_to,
t2.y as y_to
from test t1
join test t2 on t2.x = t1.x + 1 and t2.y = t1.y
union
select
t1.x as x_from, 
t1.y as y_from,
t2.x as x_to,
t2.y as y_to
from test t1
join test t2 on t2.x = t1.x - 1 and t2.y = t1.y

现在我们必须递归遍历这个视图:

with walk (x_col_from, y_col_from, x_col_to, y_col_to, stack ) as(
select 
x_from, 
y_from, 
x_to, 
y_to, 
cast(concat('|',x_from ,',',y_from, '|', x_to, ',', y_to) as varchar)
from fromto
union all
select 
walk.x_col_from, 
walk.y_col_from, 
fromto.x_to, 
fromto.y_to,
cast(concat(walk.stack,'|',fromto.x_to,',',fromto.y_to) as varchar)
from fromto
join walk on walk.x_col_to = fromto.x_from and walk.y_col_to = fromto.y_from 
where CHARINDEX(cast(concat('|',fromto.x_to,',',fromto.y_to) as varchar), walk.stack) = 0
)
select * from walk 

堆栈字段用于记忆我们已访问过的单元格。它还提供了通过网格的路径。

答案 1 :(得分:1)

我认为这不可能用SQL。您需要将数据加载到2D布尔矩阵中并实现搜索算法。您从一个点开始并递归搜索所有8个方向,并将带有X的项目添加到Set。找到空位时停止。

这有点类似于绘图程序中的区域填充算法。

您可以通过向每个坐标发出单独的SQL来直接在数据库中进行搜索,但这不会非常有效。

答案 2 :(得分:0)

这可以通过4个方向的一些递归** SQL路径搜索来完成,但我觉得你不能在单个查询中执行..来自任何X' d您可以在4个方向上使用查询步骤,如果找到X,请使用相同查询的递归调用来调查该目标的上下文..但是您需要避免循环循环和翻转邻居。在每个WHERE中,"覆盖位置的过滤器都会更加柔和。需要。你会发现每一组都只有一组。要查找所有组,您需要运行更多查询。

为了避免太多的测试,我在方形区域的边界放置一个空单元格,并迭代1..N-2而不是0..N-1。

平台:可以使用存储过程中的查询来完成工作。但我宁愿考虑来使用SQL。使用CLR调用可能更方便,大部分问题用c#编码。您可以从存储过程实现引用classlib DLL的函数。

******没看标签!在MySql中没有递归选择..所以答案是" no"反正