我有一个表,我在其中映射基于 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=3
和Y=2
它获取:(1,1),(2,1),(3,1),(4 ,1),(2,2),(3,2)
X=5
和Y=3
它获取:(5,3)
X=2
和Y=4
它获取:(2,4),(2,5)
X=7
和Y=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/