gpt4 book ai didi

sql - 在六边形区域中选择相邻单元格

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:30:20 24 4
gpt4 key购买 nike

想象一下具有 3 个维度的六边形空间。

每个图 block 都有坐标 XYZ。我需要在同一平面上选择给定的小区邻居。使用 SQL 它看起来像:

$tbDir = $y % 2 == 0 ? -1 : 1;

$result = db_query('SELECT x,y,z FROM {cells} WHERE
x = %d AND y = %d AND z = %d OR
x = %d AND y = %d AND z = %d OR
x = %d AND y = %d AND z = %d OR
x = %d AND y = %d AND z = %d OR
x = %d AND y = %d AND z = %d OR
x = %d AND y = %d AND z = %d OR
x = %d AND y = %d AND z = %d ',
$x, $y, $z,
$x-1, $y, $z,
$x+1, $y, $z,
$x, $y-1, $z,
$x, $y+1, $z,
$x+$tbDir, $y-1, $z,
$x+$tbDir, $y+1, $z);

但是,我不喜欢这样。也许有人知道更优化的算法?谢谢!

最佳答案

这看起来你可以使用 between

x BETWEEN $x-1 AND $x+1 AND y BETWEEN $y-1 AND $y+1 AND z = $z

这可能不适用于 $tbDir 部分。我将更详细地查看此案例。

好的,不如试试这个

WHERE   x BETWEEN ($x-1 AND $x+1 AND y = $y AND z = $z)
OR (y BETWEEN $y-1 AND $y+1 AND x = $x AND z = $z)
OR (y BETWEEN $y-1 AND $y+1 AND x = $x + $tbDir AND z = $z)

甚至

WHERE   (   (x BETWEEN $x-1 AND $x+1 AND y = $y )
OR (y BETWEEN $y-1 AND $y+1 AND x = $x)
OR (y BETWEEN $y-1 AND $y+1 AND x = $x + $tbDir)
)
AND z = $z

关于sql - 在六边形区域中选择相邻单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1860002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com