倒是不復(fù)雜
需求:
實(shí)現(xiàn)思路:
給定一個(gè)ID,比如272944
如果表中的ID大于這個(gè)值,則自定義變量自增加一,
如果表中的ID小于這個(gè)值,則用這個(gè)值減去表中ID,
如果表中的ID等于這個(gè)值,則為0
-
SELECT
-
a.ZpID,
-
WHEN a.zpid - 272944 > 0 THEN @a:=@a + 1
-
WHEN a.zpid - 272944 < 0 THEN a.zpid - 272944
-
ELSE 0
-
END AS rn
-
FROM
-
zp_audio_video a, (SELECT @a:=0) vars
-
WHERE
-
a.State = 1
-
AND a.Labels LIKE '三周年站慶歌曲類%'
-
ORDER BY a.Zpid
結(jié)果:
最后過(guò)濾找到結(jié)果:
-
SELECT
-
*
-
FROM
-
(
-
SELECT
-
a.ZpID,
-
WHEN a.zpid - 272944 > 0 THEN @a:=@a + 1
-
WHEN a.zpid - 272944 < 0 THEN a.zpid - 272944
-
ELSE 0
-
END AS rn
-
FROM
-
zp_audio_video a, (SELECT @a:=0) vars
-
WHERE
-
a.State = 1
-
AND a.Labels LIKE '三周年站慶歌曲類%'
-
ORDER BY a.Zpid
-
) a
-
WHERE
-
rn <= 5 AND rn != 0
-
ORDER BY rn DESC
-
LIMIT 10;
分享題目:指定一個(gè)ID找到上下的范圍
文章源于:
http://weahome.cn/article/posdcc.html