这两天一直在做社区论坛的帖子的(置顶,上移,下移,置底操作),下面是个人心得:
-- 上一条:select * from 表 where 数据id<@当前显示数据id order by 数据_id asc) limit 1
-- 下一条:select * from 表 where 数据id>@当前显示数据id order by 数据_id desc) limit 1
-- 给新查询出来的结果添加新的编号
SELECT
@row := @row +1 AS ROW ,
bestTopic.id,
bestTopic.cover,
bestTopic.title,
bestTopic.orders,
bestTopic.topic_id,
bestTopic.is_pub,
bestTopic.create_date,
bestTopic.modify_date
from
qbao_bbs_best_topic bestTopic,
(
SELECT @row :=0
)r
where
1=1
ORDER BY
orders desc
-- 最大编号
SELECT IFNULL(orders,0) from qbao_bbs_best_topic ORDER BY orders desc limit 1;
-- 最小编号
SELECT orders from qbao_bbs_best_topic ORDER BY orders asc limit 1;
-- 下一条
SELECT
IFNULL(orders,0)
from
(
SELECT
@row := @row +1 AS ROW ,
id,
orders
from
qbao_bbs_best_topic,
(
SELECT @row :=0
)r
ORDER BY
orders desc
) bestTopic
where ROW<1
ORDER BY ROW DESC limit 1;
-- 上一条
SELECT
orders
from
(
SELECT
@row := @row +1 AS ROW ,
id,
orders
from
qbao_bbs_best_topic,
(
SELECT @row :=0
)r
ORDER BY
orders desc
) bestTopic
where ROW>1
ORDER BY ROW ASC limit 1;
-- 数据前提
1)数据存在字段(orders),每次新增,编号默认(最大值+1)
2)数据编号,会存在相同编号,以及负数编号
3)置顶,置底,操作针对所有记录,(所有页面,不是单个页面)
-- 操作需求
置顶,上一条,下一条,置底
-- 规则
1)置顶操作 ,被操作记录 编号是置顶帖子(编号+1);
2)置底操作 ,被操作记录 编号是置底帖子(编号-1);
3)上一条记录,被操作记录,与上一条记录比较:
1.如果编号不同,互相调换位置;
2.如果编号相同,上一条操作记录(编号-1);
4)下一条记录,被操作记录,与下一条记录比较:
1.如果编号不同,互相调换位置;
2.如果编号相同,下一条操作记录,(编号+1);