mysql - 如何查询连通坐标?

标签 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);

最佳答案

你问的可以通过普通表表达式实现。

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

让我们首先从列出所有相邻单元格的 View 开始:

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

现在我们必须递归地遍历这个 View :

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 

堆栈字段用于内存我们已经访问过的单元格。它还给出了通过网格的路径。

关于mysql - 如何查询连通坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38425130/

相关文章:

mysql - 使用 If 结构的约束

asp.net - 如何在 SQL Server 表中创建图像列并使用 VB 在 ASP.NET 中检索它

mysql - DATETIME 默认值 0 MYSQL 5.5.25

php - 如果选择标签的选项值不是特定值,我如何设置默认值?

mysql - 将表中日期时间的所有列转换为不同的日期格式

mysql - SQL查询没有得到正确的支付方式

sql - 关于E-R建模的问题

php - 如何设置 php 以便将数据插入数据库?

mysql - 使用 groupby 子句从多个表检索数据显示 "not a groupby expression"

mysql - 计算引用同一表中主键的外键的出现次数