SELECT t.Padal, u.Nool
FROM (SELECT m.*, @n1:=@n1 + 1 num,
@n2:=IF(Padal = 'பசிப் பிடி உதைத்த ஓமைச் செவ் வரை' , @
n1, @n2) pos
FROM tbl_padal m, (SELECT @n1:=0, @n2:=0) n
ORDER BY m.uqPadalid
) t, tbl_Nool u
WHERE t.uqnoolId = u.uqnoolID
and u.nool = 'நற்றிணை'
and @n2 >= num - 2
AND @n2 <= num + 2
我想要获得两个记录(总共五个记录)的上方和下方
答案 0 :(得分:0)
@mcbalaji 对于大纲,让我们从简单和已知的步骤逐步构建sql。 创建并运行其中的每一个 -
1st SELECT m.* From tbl_padal m (note that it is not ordered by uqPadalid)
2nd SELECT m.* From tbl_padal m ORDER By uqPadalid (now it is ordered)
3rd SELECT
(SELECT Count(*) + 1 FROM tbl_padal AS A WHERE A.uqPadalid < m.uqPadalid ) as RowNum,
m.* From tbl_padal m ORDER By m.uqPadalid (now it also has RowNum-s)
4th So lets pick the central one that we want--
SELECT RowNum FROM
( SELECT
(SELECT Count(*) + 1 FROM tbl_padal AS A WHERE A.uqPadalid < m.uqPadalid ) as RowNum,
m.Padal,
m.uqPadalid From tbl_padal m ORDER By m.uqPadalid
)
Where m.Padal = 'your constant'
Lastly--
SELECT
nums.RowNum,
mm.RowNum,
mm.Padal,
mm.uqPadalid
u.nool
FROM (
( SELECT RowNum, uqPadalid FROM
( SELECT
(SELECT Count(*) + 1 FROM tbl_padal AS A WHERE A.uqPadalid < m.uqPadalid ) as RowNum,
m.Padal,
m.uqPadalid From tbl_padal m ORDER By m.uqPadalid
)
Where m.Padal = 'constant1'
) as nums
LEFT JOIN
( SELECT
(SELECT Count(*) + 1 FROM tbl_padal AS A WHERE A.uqPadalid < m.uqPadalid ) as RowNum,
m.Padal,
m.uqPadalid From tbl_padal m ORDER By m.uqPadalid
) as mm
ON mm.RowNum <= nums.RowNum +2
AND nums.Rownum -2 <= mm.RowNum )
LEFT JOIN
tbl_nool u
Where mm.uqPadalid = u.uqPadalid
And u.nool = 'constant2'
Order By mm.RowNum