我有一个具有id的表,以及表示一系列数字的开始/结束值。我期待在现有范围之间找到特定尺寸(或更大)的第一个间隙。例如:
| id | start | end |
|-------|-------|-----|
| xz132 | 2 | 5 |
| fd754 | 9 | 12 |
| sb825 | 16 | 23 |
(编辑:ID不是增量的)
如果我正在寻找大小为1的第一个间隙,它应该返回1-1的范围。
如果我正在寻找大小为3的差距,它应该返回范围6-8可用。
如果我正在寻找大小为6的间隙,它应该返回null表示没有间隙,或者24-29表示在表格末尾添加另一条记录。
答案 0 :(得分:2)
以下是使用变量的解决方案:
select @end+1 start, start-1 end
from (select * from ranges order by start) as base,
(select @gap := 3, @end := 0) as init
where if(@gap < start - @end, 1, if(@end := end, 0, 0))
limit 1;
将分配给@gap
的号码替换为所需的间隙大小。
对于@gap := 1
,它返回(1,1)
对于@gap := 3
,它返回(6,8)
对于@gap := 4
,它不返回任何数据
答案 1 :(得分:0)
SELECT * FROM t WHERE (t.end+1)-t.start >= gap LIMIT 1;
<强>解释强>
t 是您的表格的名称
要获得差距,您需要从结尾减去开头并添加1。
差距是您选择的差距
LIMIT 1
会为您提供第一个结果。
演示here
答案 2 :(得分:0)
考虑到ID可能不连续的事实;我需要一个技巧来允许在第一个记录之前进行搜索,这就是为什么我&#34;插入&#34;在飞行中虚拟伪零记录。当然,只要class FrameBuffer(object):
# ... omitted for brevity
@to_texture
def draw_circle(self, x, y, r, color, opacity=1.0, fill=True, aa=False, penwidth=1):
# Make sure all spatial parameters are ints
x, y, r = int(x), int(y), int(r)
# convert color parameter to sdl2 understandable values
color = sdl2.ext.convert_to_color(color)
# convert possible opacity values to the right scale
opacity = self.opacity(opacity)
# Check for invalid penwidth values, and make sure the value is an int
if penwidth != 1:
if penwidth < 1:
raise ValueError("Penwidth cannot be smaller than 1")
if penwidth > 1:
penwidth = int(penwidth)
# if only a filled circle needs to be drawn, it's easy
if fill:
sdlgfx.filledCircleRGBA(self.sdl_renderer, x, y, r, color.r, color.g, color.b, opacity)
else:
# If penwidth is 1, simply use sdl2gfx's own functions
if penwidth == 1:
if aa:
sdlgfx.aacircleRGBA(self.sdl_renderer, x, y, r, color.r, color.g, color.b, opacity)
else:
sdlgfx.circleRGBA(self.sdl_renderer, x, y, r, color.r, color.g, color.b, opacity)
else:
# If the penwidth is larger than 1, things become a bit more complex.
# To ensure that the interior of the circle is transparent, we will
# have to work with multiple textures and blending.
outer_r, inner_r = int(r+penwidth*.5), int(r-penwidth*.5)
# Calculate the required dimensions of the extra texture we are
# going to draw the circle on. Add 1 pixel to account for division
# errors (i.e. dividing an odd number of pixels)
c_width, c_height = outer_r*2+1, outer_r*2+1
# Create the circle texture, and make sure it can be a rendering
# target by setting the correct access flag.
circle_sprite = self.environment.texture_factory.create_software_sprite(
size=(c_width, c_height),
)
# Create a renderer to draw to the circle sprite
sprite_renderer = sdl2.ext.Renderer(circle_sprite)
# Determine the optimal color key to use for this operation
colorkey_color = self.determine_optimal_colorkey(color, self.background_color)
# Clear the sprite with the colorkey color
sprite_renderer.clear(colorkey_color)
# Draw the annulus:
sdlgfx.filledCircleRGBA(sprite_renderer.sdlrenderer, outer_r, outer_r,
outer_r, color.r, color.g, color.b, 255)
# Antialias if desired
if aa:
for i in range(-1,1):
for j in range(2):
sdlgfx.aacircleRGBA(sprite_renderer.sdlrenderer, outer_r,
outer_r, outer_r+i, color.r, color.g, color.b, 255)
# Draw the hole
sdlgfx.filledCircleRGBA(sprite_renderer.sdlrenderer, outer_r, outer_r,
inner_r, colorkey_color.r, colorkey_color.g, colorkey_color.b, 255)
# Antialias if desired
if aa:
for i in range(0,2):
for j in range(2):
sdlgfx.aacircleRGBA(sprite_renderer.sdlrenderer, outer_r,
outer_r, inner_r+i, color.r, color.g, color.b, 255)
# Optimize drawing of transparent pixels
sdl2.SDL_SetSurfaceRLE(circle_sprite.surface, 1)
# Convert the colorkey to a format understandable by the
# SDL_SetColorKey function
colorkey = sdl2.SDL_MapRGB(circle_sprite.surface.format,
colorkey_color.r, colorkey_color.g, colorkey_color.b)
# Set transparency color key
sdl2.SDL_SetColorKey(circle_sprite.surface, sdl2.SDL_TRUE, colorkey)
# Create a texture from the circle sprite
circle_texture = self.environment.texture_factory.from_surface(
circle_sprite.surface
)
# Set the desired transparency value to the texture
sdl2.SDL_SetTextureAlphaMod(circle_texture.texture, opacity)
# Perform the blitting operation
self.renderer.copy( circle_texture, dstrect=(x-int(c_width/2),
y-int(c_height/2), c_width, c_height) )
# Cleanup
del(circle_sprite)
del(sprite_renderer)
return self
def determine_optimal_colorkey(self, drawing_color, dev_offset=5):
""" Determines the optimal color to use for the transparent color key."""
# If foreground and background colors are not identical, simply return
# the background color
if drawing_color != self.background_color:
return self.background_color
if not 1 <= dev_offset <= 25:
raise ValueError("dev_offset should be a value between 1 and 25")
# Create offset_color
offset_color = sdl2.ext.Color(dev_offset, dev_offset, dev_offset, 0)
# Create white_color
white = sdl2.ext.Color(255,255,255,255)
color_key = self.background_color + offset_color
if color_key != white:
return color_key
else:
return self.background_color - offset_color
def opacity(self, value):
""" Convert float values to opacity range between 0 and 255. """
if type(value) == float and 0.0 <= value <= 1.0:
# This is maybe a bit iffy, because it does not allow opacity values
# in the 0 to 255 range between 0 and 1 (it maybe undesiredly converts
# it to value*255).
# TODO: Think about a solution for this
return int(value*255)
elif type(value) in [int, float]:
if 0 <= value <= 255:
return int(value)
else:
raise ValueError("Invalid opacity value")
else:
raise TypeError("Incorrect type or value passed for opacity.")
# ... ommitted for brevity
;否则你应该使用另一个值,或另一个技巧:-)。 &#34; 3&#34;应该是一个参数。
ID >= 1
答案 3 :(得分:0)
你可以使用这个简单的查询:
select * from `test`.`test_1` as out1 where `to`
< ( select min( `from` ) from `test`.`test_1` as in1 where in1.`from`
> out1.`to` )+ XX;
你可以玩XXX来获得你想要的差距,这会给你所有这些,你可以使用“从...开始”然后“限制1”来获得第一个