How to select non-consecutive rows in MySQL?

前端 未结 5 1519
终归单人心
终归单人心 2021-01-13 07:08

If the primary keys of the records are 1,3,4,5,6,8

I want to select the records with pk:1,6

NOTE

I don\'t

5条回答
  •  无人共我
    2021-01-13 07:51

    A little improvement for the solution proposed by Robin Day

    SELECT
        [MyId] + 1
    FROM
        [MyTable]
    WHERE
        [MyId] NOT IN
    (
        SELECT
            [MyId] - 1
        FROM
            [MyTable]
    )
    ORDER BY [MyId] + 1
    

提交回复
热议问题