日期:2014-05-16  浏览次数:20887 次

mysql 查询左右数据
现在有一条a的数据, 我想查询出来以a为中心,a前面两天数据和a后面两条数据结合起来的5条数据。要怎么实现呢。?

------解决方案--------------------
(select * from table1 where id>=(select id from table1 where name='dave') order by id limit 3)
union all
(select * from table1 where id<(select id from table1 where name='dave') order by id limit 2)
------解决方案--------------------
select * from (
(SELECT `year` FROM wt_brandnotes WHERE `year`<2009 GROUP BY `year` ORDER BY `year` DESC LIMIT 2)


UNION ALL


(SELECT `year` FROM wt_brandnotes WHERE `year`>=2009 GROUP BY `year` ORDER BY `year` LIMIT 3)
) a order by `year`