日期:2014-05-17  浏览次数:21259 次

考考大家,给出一个最简单的条件语句SQL!
A表里存着n个对象的坐标范围信息,left\top\right\bottom表示对象的坐标范围(矩形),(left,bottom)左下角坐标,(right,top)右上角坐标,如果已知一个矩形范围(left1,bottom1),(right1,top1),那么如何用最简单的条件语句查出A表中和这个矩形范围有交集的对象?
注意:是有交集的对象,也就是只要是有交集的对象都要查出来。

------解决方案--------------------
不在上方 not bottom>top1
不在下方 not top<bottom1
不在左侧 not right<left1
不在右侧 not left>right1
bottom<=top1 and top>=bottom1 and right>=left1 and left<=right1
索引就别想了,都是所有字段都是单向比较,如果索引结果量过多,还不如没有索引。
------解决方案--------------------
select * from a where ((left1>left and left1<right) or (right1>left and right1<right))
and ((top1>bottom and top1<top) or (bottom1>bottom and bottom1<top))
可以把上面的语句分次4个进行union,索引应该能够起到一定作用吧
------解决方案--------------------
一句话的事:
select * from a where left <= right1
                 and right >= left1
                and bottom <= top1
                   and top >= bottom1;
------解决方案--------------------
晕,前面已经有相同答案了!
按left\top\right\bottom四个字段建立一个索引的话,性能不是问题!