如何使用 CURSOR 在 MySQL 中获取两条上一条记录

How to get TWO Previous record in MySQL using CURSOR

在我的 table 上,我有包含“,”的记录s

Id           Name
1            Here is the result
2            of your examination.
3            ,  
4            New Opening for the position of 
5            PT Teacher, Science Lab.
6            ,     

所以在游标中,如果我找到“,”,那么我想将 2 行值合并到第一行中。

DECLARE @ID int
DECLARE @Name nvarchar(500)

DECLARE MergeCursor CURSOR FOR 
select ID,NAME from  TEST_TABLE 

OPEN MergeCursor
FETCH NEXT FROM NarrationCursor into @ID,@NAME

WHILE (@@FETCH_STATUS=0)
BEGIN 
    
    if(@Name = ',')
        select * from TEST_TABLE where ID = (select max(ID) from TEST_TABLE where ID < @ID)
    
    FETCH NEXT FROM NarrationCursor into @ID,@NAME
END

CLOSE MergeCursor
DEALLOCATE MergeCursor

IN 游标如何获取前两行并更新第一行中的值并删除第二行和第三行。以及更新 ID

最后我要输出

Id           Name
1            Here is the result of your examination.
2            New Opening for the position of PT Teacher, Science Lab. 
WITH
  grouped AS
(
  SELECT
    SUM(CASE WHEN name=',' THEN 1 ELSE 0 END)
      OVER (ORDER BY id)
        AS group_id,
    id,
    name
  FROM
    TEST_TABLE
)
SELECT
  group_id + 1   AS id,
  STRING_AGG(name, ' ') WITHIN GROUP (ORDER BY id)   AS name
FROM
  grouped
WHERE
  name <> ','
GROUP BY
  group_id
ORDER BY
  group_id